Interface Map<K,V>
- All Known Subinterfaces:
ConcurrentMap<K,V>,ConcurrentNavigableMap<K,V>,NavigableMap<K,V>,SortedMap<K,V>
- All Known Implementing Classes:
AbstractMap,Attributes,AuthProvider,BouncyCastleProvider,ConcurrentHashMap,ConcurrentSkipListMap,CountersTable,CryptoProvider,DRLCertFactory,EnumMap,HashMap,Hashtable,IdentityHashMap,JSSEProvider,LinkedHashMap,OpenSSLProvider,Properties,Provider,TreeMap,WeakHashMap,WeakHashtable
public interface Map<K,V>
Map is a data structure consisting of a set of keys and values
in which each key is mapped to a single value. The class of the objects
used as keys is declared when the Map is declared, as is the
class of the corresponding values.
A Map provides helper methods to iterate through all of the
keys contained in it, as well as various methods to access and update
the key/value pairs.
-
Nested Class Summary
-
Method Summary
Modifier and Type Method Description voidclear()Removes all elements from thisMap, leaving it empty.booleancontainsKey(Object key)Returns whether thisMapcontains the specified key.booleancontainsValue(Object value)Returns whether thisMapcontains the specified value.Set<Map.Entry<K,V>>entrySet()Returns aSetcontaining all of the mappings in thisMap.booleanequals(Object object)Compares the argument to the receiver, and returnstrueif the specified object is aMapand bothMaps contain the same mappings.Vget(Object key)Returns the value of the mapping with the specified key.inthashCode()Returns an integer hash code for the receiver.booleanisEmpty()Returns whether this map is empty.Set<K>keySet()Returns a set of the keys contained in thisMap.Vput(K key, V value)Maps the specified key to the specified value.voidputAll(Map<? extends K,? extends V> map)Copies every mapping in the specifiedMapto thisMap.Vremove(Object key)Removes a mapping with the specified key from thisMap.intsize()Returns the number of mappings in thisMap.Collection<V>values()Returns aCollectionof the values contained in thisMap.
-
Method Details
-
clear
void clear()Removes all elements from thisMap, leaving it empty.- Throws:
UnsupportedOperationException- if removing elements from thisMapis not supported.- See Also:
isEmpty(),size()
-
containsKey
Returns whether thisMapcontains the specified key.- Parameters:
key- the key to search for.- Returns:
trueif this map contains the specified key,falseotherwise.
-
containsValue
Returns whether thisMapcontains the specified value.- Parameters:
value- the value to search for.- Returns:
trueif this map contains the specified value,falseotherwise.
-
entrySet
Returns aSetcontaining all of the mappings in thisMap. Each mapping is an instance ofMap.Entry. As theSetis backed by thisMap, changes in one will be reflected in the other.- Returns:
- a set of the mappings
-
equals
Compares the argument to the receiver, and returnstrueif the specified object is aMapand bothMaps contain the same mappings.- Overrides:
equalsin classObject- Parameters:
object- theObjectto compare with thisObject.- Returns:
- boolean
trueif theObjectis the same as thisObjectfalseif it is different from thisObject. - See Also:
hashCode(),entrySet()
-
get
Returns the value of the mapping with the specified key.- Parameters:
key- the key.- Returns:
- the value of the mapping with the specified key, or
nullif no mapping for the specified key is found.
-
hashCode
int hashCode()Returns an integer hash code for the receiver.Objects which are equal return the same value for this method.- Overrides:
hashCodein classObject- Returns:
- the receiver's hash.
- See Also:
equals(Object)
-
isEmpty
boolean isEmpty()Returns whether this map is empty.- Returns:
trueif this map has no elements,falseotherwise.- See Also:
size()
-
keySet
Returns a set of the keys contained in thisMap. TheSetis backed by thisMapso changes to one are reflected by the other. TheSetdoes not support adding.- Returns:
- a set of the keys.
-
put
Maps the specified key to the specified value.- Parameters:
key- the key.value- the value.- Returns:
- the value of any previous mapping with the specified key or
nullif there was no mapping. - Throws:
UnsupportedOperationException- if adding to thisMapis not supported.ClassCastException- if the class of the key or value is inappropriate for thisMap.IllegalArgumentException- if the key or value cannot be added to thisMap.NullPointerException- if the key or value isnulland thisMapdoes not supportnullkeys or values.
-
putAll
Copies every mapping in the specifiedMapto thisMap.- Parameters:
map- theMapto copy mappings from.- Throws:
UnsupportedOperationException- if adding to thisMapis not supported.ClassCastException- if the class of a key or a value of the specifiedMapis inappropriate for thisMap.IllegalArgumentException- if a key or value cannot be added to thisMap.NullPointerException- if a key or value isnulland thisMapdoes not supportnullkeys or values.
-
remove
Removes a mapping with the specified key from thisMap.- Parameters:
key- the key of the mapping to remove.- Returns:
- the value of the removed mapping or
nullif no mapping for the specified key was found. - Throws:
UnsupportedOperationException- if removing from thisMapis not supported.
-
size
int size()Returns the number of mappings in thisMap.- Returns:
- the number of mappings in this
Map.
-
values
Collection<V> values()Returns aCollectionof the values contained in thisMap. TheCollectionis backed by thisMapso changes to one are reflected by the other. TheCollectionsupportsCollection.remove(java.lang.Object),Collection.removeAll(java.util.Collection<?>),Collection.retainAll(java.util.Collection<?>), andCollection.clear()operations, and it does not supportCollection.add(E)orCollection.addAll(java.util.Collection<? extends E>)operations.This method returns a
Collectionwhich is the subclass ofAbstractCollection. TheAbstractCollection.iterator()method of this subclass returns a "wrapper object" over the iterator of thisMap'sentrySet(). TheAbstractCollection.size()method wraps thisMap'ssize()method and theAbstractCollection.contains(java.lang.Object)method wraps thisMap'scontainsValue(java.lang.Object)method.The collection is created when this method is called at first time and returned in response to all subsequent calls. This method may return different Collection when multiple calls to this method, since it has no synchronization performed.
- Returns:
- a collection of the values contained in this map.
-