public Vget(Object key)
Returns the object to which key is mapped, or null if it is not mapped.
Also returns null if key has been mapped to null in a map that allows
null values. You can use containsKey to distinguish between the cases,
although this adds overhead. It can be more efficient to put marker objects
instead of null into the map to avoid the need for the second test.
public Vput(K key, V value)
Associates key with the given value in the map. If a map already exists for
key, its value is changed and the original value is returned. If no mapping
exists, put returns null, which may also mean that key was originally
mapped to null. (Optional)
public Vremove(Object key)
Removes any mapping for the key. The return value has the same semantics
as that of put. (Optional)
public voidputAll(Map<? extends K,? extends V> otherMap)
Puts all the mappings in otherMap into this map. (Optional)
public voidclear()
Removes all mappings. (Optional)
Methods that take keys as parameters may throw ClassCastException if the key is not of the
appropriate type for this map, or NullPointerException if the key is null and this map does not
accept null keys.
You can see that, though Map does not extend Collection, methods with the same meaning have the same
names, and analogous methods have analogous names. This helps you remember the methods and what they
do.
Generally, you can expect a Map to be optimized for finding values listed under keys. For example, the
method containsKey will usually be much more efficient than containsValue on larger maps. In a
HashMap, for example, containsKey is 0(1), whereas containsValue is 0(n) the key is found by
hashing, but the value must be found by searching through each element until a match is found.
Map is not a collection, but there are methods that let you view the map using collections:
public Set<K>keySet()
Returns a Set whose elements are the keys of this map.
public Collection<V>values()
Returns a Collection whose elements are the values of this map.
public Set<Map.Entry<K,V>>enTRySet()
Returns a Set whose elements are Map.Entry objects that represent single