Java Reference
In-Depth Information
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 ex-
ample, 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 ele-
ment 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 rep-
resent single mappings in the map. As you will see soon,
Map.Entry is a nested interface with methods to manipulate the
entry.
The collections returned by these methods are backed by the Map , so
removing an element from one of these collections removes the cor-
responding key/value pair from the map. You cannot add elements to
these collectionsthey do not support the optional methods for adding to
a collection. If you iterate through the key and value sets in parallel you
cannot rely on getting key/value pairsthey may return values from their
respective sets in any order. If you need such pairing, you should use
entrySet .
 
Search WWH ::




Custom Search