Java Reference
In-Depth Information
Note that the get() operation returns a reference to the object associated with the key, but it does not
remove it from the table. To retrieve an object and delete the entry containing it from the table, you must use
the remove() method. This removes the object corresponding to the key and returns a reference to it:
int objectValue = aMap.remove(myKey);
As noted previously, if there's no stored object corresponding to myKey , or null was stored as the object,
null is returned. If you were to append this statement to the previous fragment, a reference to an Integer
object encapsulating the value 12348 would be returned. Because you store it in a variable of type int , the
compiler inserts an unboxing conversion for the return value.
Processing All the Elements in a Map
The Map<> interface provides three ways of obtaining a collection view of the contents of a map. You can ob-
tain all the keys from a Map<K,V> object as an object of type Set<K> .You can also get a Collection<V> ob-
ject that contains all the values in the map. Key/object pairs are stored in a map as objects of a type that im-
plements the Map.Entry<K,V> interface. This is a generic interface type that is defined within the Map<K,V>
interface. You can get all the key/object pairs from the map as an object of type Set<Map.Entry<K,V>> .
Note that the Set<> or Collection<> object that you get is essentially a view of the contents of a map,
so changes to a HashMap<> object are reflected in the associated Set<> or Collection<> , and vice versa .
The three methods involved are the following:
Set<K> keySet() : Returns an object containing all the keys from the map.
Set<Map.Entry<K,V>> entrySet() : Returns an object containing the key/object pairs — each
pair being an object of type Map.Entry<K,V> .
Collection<V> values() : Returns an object containing all the values stored in the map.
Let's first see how you can use a set of keys. The keySet() method for a HashMap<K,V> object returns a
Set<K> object containing the set of keys that you can either use directly to access the keys or use indirectly
to get at the objects stored in the map. For a HashMap<String, Integer> object aMap , you could get the
set of all the keys in the map with the following statement:
Set<String> keys = aMap.keySet();
Now you can get an iterator for this set of keys with this statement:
Iterator<String> keyIter = keys.iterator();
You can use the iterator() method for the object keys to iterate over all the keys in the map. Of course,
you can combine these two operations to get the iterator directly. For example:
Iterator<String> keyIter = aMap.keySet().iterator(); // Get the iterator
while(keyIter.hasNext()) { // Iterate over the keys
System.out.println(keyIter.next());
}
This iterates over all the keys and outputs them.
The Set<> interface has Iterable<> as a superinterface, so you can use the collection-based for loop
directly with the object that the keySet() method returns:
Search WWH ::




Custom Search