Method
Description
void clear( )
Removes all key/value pairs from the invoking map.
boolean containsKey(Object k)
Returns true if the invoking map contains k as a key. Other wise,
returns false.
boolean containsValue(Object v)
Returns true if the map contains v as a value. Other wise, returns false.
Set<Map.Entr y<K, V>> entr ySet( ) Returns a Set that contains the entries in the map. The set contains
objects of type Map.Entry. Thus, this method provides a set-view of the
invoking map.
boolean equals(Object obj)
Returns true if obj is a Map and contains the same entries. Other wise,
returns false.
V get(Object k)
Returns the value associated with the key k. Returns null if the key is
not found.
int hashCode( )
Returns the hash code for the invoking map.
boolean isEmpty( )
Returns true if the invoking map is empty. Other wise, returns false.
Set<K> keySet( )
Returns a Set that contains the keys in the invoking map. This method
provides a set-view of the keys in the invoking map.
V put(K k, V v)
Puts an entr y in the invoking map, over writing any previous value
associated with the key. The key and value are k and v, respectively.
Returns null if the key did not already exist. Other wise, the previous
value linked to the key is returned.
Puts all the entries from m into this map.
void putAll(Map<? extends K,
void putAll(Map<? extends V> m)
V remove(Object k)
Removes the entr y whose key equals k.
int size( )
Returns the number of key/value pairs in the map.
Collection<V> values( )
Returns a collection containing the values in the map. This method
provides a collection-view of the values in the map.
TABLE 17-10
The Methods Defined by Map
use keySet( ). To get a collection-view of the values, use values( ). Collection-views are the
means by which maps are integrated into the larger Collections Framework.
The SortedMap Interface
The SortedMap interface extends Map. It ensures that the entries are maintained in ascending
order based on the keys. SortedMap is generic and is declared as shown here:
interface SortedMap<K, V>
Here, K specifies the type of keys, and V specifies the type of values.
The methods declared by SortedMap are summarized in Table 17-11. Several methods throw
a NoSuchElementException when no items are in the invoking map. A ClassCastException
is thrown when an object is incompatible with the elements in a map. A NullPointerException
is thrown if an attempt is made to use a null object when null is not allowed in the map. An
IllegalArgumentException is thrown if an invalid argument is used.
Sorted maps allow very efficient manipulations of submaps (in other words, subsets of a
map). To obtain a submap, use headMap( ), tailMap( ), or subMap( ). To get the first key in
the set, call firstKey( ). To get the last key, use lastKey( ).
Search WWH :