466 Part II: The Java Library
The NavigableMap Interface
TheNavigableMapinterface was added by Java SE 6. It extendsSortedMapand declares
the behavior of a map that supports the retrieval of entries based on the closest match to a
given key or keys.NavigableMapis a generic interface that has this declaration:
interface NavigableMap<K,V>
Here,Kspecifies the type of the keys, andVspecifies the type of the values associated with
the keys. In addition to the methods that it inherits fromSortedMap,NavigableMapadds
those summarized in Table 17-12. Several methods throw aClassCastExceptionwhen
an object is incompatible with the keys in the map. ANullPointerExceptionis thrown
if an attempt is made to use anullobject andnullkeys are not allowed in the set. An
IllegalArgumentExceptionis thrown if an invalid argument is used.
Method Description
Comparator<? super K> comparator( ) Returns the invoking sor ted map’s comparator. If natural
ordering is used for the invoking map,nullis returned.
K firstKey( ) Returns the first key in the invoking map.
Sor tedMap<K, V> headMap(Kend) Returns a sor ted map for those map entries with keys that are
less thanend.
K lastKey( ) Returns the last key in the invoking map.
Sor tedMap<K, V> subMap(Kstar t, Kend) Returns a map containing those entries with keys that are
greater than or equal tostar tand less thanend.
Sor tedMap<K, V> tailMap(Kstar t) Returns a map containing those entries with keys that are
greater than or equal tostar t.
TABLE 17-11 The Methods Defined bySortedMap
Method Description
Map.Entr y<K,V> ceilingEntr y(Kobj) Searches the map for the smallest keyksuch thatk>=obj. If such a key
is found, its entr y is returned. Other wise,nullis returned.
K ceilingKey(Kobj) Searches the map for the smallest keyksuch thatk>=obj. If such a key
is found, it is returned. Other wise,nullis returned.
NavigableSet<K> descendingKeySet( ) Returns aNavigableSetthat contains the keys in the invoking map in
reverse order. Thus, it returns a reverse set-view of the keys. The
resulting set is backed by the map.
NavigableMap<K,V> descendingMap( ) Returns aNavigableMapthat is the reverse of the invoking map. The
resulting map is backed by the invoking map.
Map.Entr y<K,V> firstEntr y( ) Returns the first entr y in the map. This is the entr y with the least key.
Map.Entr y<K,V> floorEntr y(Kobj) Searches the map for the largest keyksuch thatk<=obj. If such a key
is found, its entr y is returned. Other wise,nullis returned.
K floorKey(Kobj) Searches the map for the largest keyksuch thatk<=obj. If such a key
is found, it is returned. Other wise,nullis returned.
NavigableMap<K,V>
headMap(KupperBound, booleanincl)
Returns aNavigableMapthat includes all entries from the invoking map
that have keys that are less thanupperBound. Ifinclistrue, then an
element equal toupperBoundis included. The resulting map is backed by
the invoking map.
Map.Entr y<K,V> higherEntr y(Kobj) Searches the set for the largest keyksuch thatk>obj. If such a key is
found, its entr y is returned. Other wise,nullis returned.
TABLE 17-12 The Methods defined byNavigableMap