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 corresponding 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.
The interface Map.Entry<K,V> defines methods for manipulating the entries in the map, as returned from
the Map interface's entrySet method:
public KgetKey()
Returns the key for this entry.
public VgetValue()
Returns the value for this entry.
public VsetValue(V value)
Sets the value for this entry and returns the old value.
Note that there is no setKey methodyou change the key by removing the existing mapping for the original
key and adding another mapping under the new key.
The SortedMap interface extends Map refining the contract to require that the keys be sorted. This ordering
requirement affects the collections returned by keySet, values, and enTRySet. SortedMap also adds
methods that make sense in an ordered map:
public Comparator<? super K>comparator()
Returns the comparator being used for sorting this map. Returns null if
none is being used, which means that the map is sorted using the keys' natural
order.
public KfirstKey()
Returns the first (lowest value) key in this map.
public KlastKey()
Returns the last (highest value) key in this map.
public SortedMap<K,V>subMap(K minKey, K maxKey)
Returns a view of the portion of the map whose keys are greater than or equal
to minKey and less than maxKey.
public SortedMap<K,V>headMap(K maxKey)
Returns a view of the portion of the map whose keys are less than maxKey.