Java The Complete Reference, Seventh Edition

(Greg DeLong) #1

The Map.Entry Interface
TheMap.Entryinterface enables you to work with a map entry. Recall that theentrySet( )
method declared by theMapinterface returns aSetcontaining the map entries. Each of these
set elements is aMap.Entryobject.Map.Entryis generic and is declared like this:


interface Map.Entry<K, V>

Here,Kspecifies the type of keys, andVspecifies the type of values. Table 17-13 summarizes
the methods declared byMap.Entry. Various exceptions are possible.


Chapter 17: java.util Part 1: The Collections Framework 467


Method Description
boolean equals(Objectobj) Returnstrueifobjis aMap.Entrywhose key and value are equal to that of the
invoking object.
K getKey( ) Returns the key for this map entr y.
V getValue( ) Returns the value for this map entr y.
int hashCode( ) Returns the hash code for this map entr y.
V setValue(Vv) Sets the value for this map entr y tov.AClassCastExceptionis thrown ifvis not
the correct type for the map. AnIllegalArgumentExceptionis thrown if there is
a problem withv.ANullPointerExceptionis thrown ifvisnulland the map does
not permitnullkeys. AnUnsupportedOperationExceptionis thrown if the map
cannot be changed.

TABLE 17-13 The Methods Defined byMap.Entry


Method Description
K higherKey(Kobj) Searches the set for the largest keyksuch thatk>obj. If such a key is
found, it is returned. Other wise,nullis returned.
Map.Entr y<K,V> lastEntr y( ) Returns the last entr y in the map. This is the entr y with the largest key.
Map.Entr y<K,V> lowerEntr 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.
K lowerKey(Kobj) Searches the set for the largest keyksuch thatk<obj. If such a key is
found, it is returned. Other wise,nullis returned.
NavigableSet<K> navigableKeySet( ) Returns aNavigableSetthat contains the keys in the invoking map. The
resulting set is backed by the invoking map.
Map.Entr y<K,V> pollFirstEntr y( ) Returns the first entr y, removing the entr y in the process. Because the
map is sorted, this is the entr y with the least key value.nullis returned if
the map is empty.
Map.Entr y<K,V> pollLastEntr y( ) Returns the last entr y, removing the entr y in the process. Because the
map is sorted, this is the entr y with the greatest key value.nullis
returned if the map is empty.
NavigableMap<K,V>
subMap(KlowerBound,
booleanlowIncl,
KupperBound
booleanhighIncl)

Returns aNavigableMapthat includes all entries from the invoking map
that have keys that are greater thanlowerBoundand less than
upperBound. IflowInclistrue, then an element equal tolowerBoundis
included. IfhighInclistrue, then an element equal tohighInclis included.
The resulting map is backed by the invoking map.
NavigableMap<K,V>
tailMap(KlowerBound, booleanincl)

Returns aNavigableMapthat includes all entries from the invoking map
that have keys that are greater thanlowerBound. Ifinclistrue, then an
element equal tolowerBoundis included. The resulting map is backed by
the invoking map.

TABLE 17-12 The Methods defined byNavigableMap(continued)

Free download pdf