Interface Map<K,V>
- All Known Subinterfaces:
NavigableMap<K,V>, SortedMap<K, V>
- All Known Implementing Classes:
AbstractMap, HashMap, Hashtable, IdentityHashMap, LinkedHashMap, Properties, TreeMap, WeakHashMap
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
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()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.entrySet()Returns aSetcontaining all of the mappings in thisMap.booleanCompares the argument to the receiver, and returnstrueif the specified object is aMapand bothMaps contain the same mappings.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.keySet()Returns a set of the keys contained in thisMap.Maps the specified key to the specified value.voidCopies every mapping in the specifiedMapto thisMap.Removes a mapping with the specified key from thisMap.intsize()Returns the number of mappings in thisMap.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:
-
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
-
equals
Compares the argument to the receiver, and returnstrueif the specified object is aMapand bothMaps contain the same mappings. -
get
-
hashCode
-
isEmpty
boolean isEmpty()Returns whether this map is empty.- Returns:
trueif this map has no elements,falseotherwise.- See Also:
-
keySet
-
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(Object),Collection.removeAll(Collection),Collection.retainAll(Collection), andCollection.clear()operations, and it does not supportCollection.add(E)orCollection.addAll(Collection)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(Object)method wraps thisMap'scontainsValue(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.
-