Showing posts with label collection-iteration. Show all posts
Showing posts with label collection-iteration. Show all posts

Friday, 27 May 2011

Collection views for Maps for iteration

The Collection-view methods allow a Map to be viewed as a Collection in three ways:
  • keySet: the Set of keys contained in the Map.
  • values: The Collection of values contained in the Map. This Collection is not a Set, as multiple keys can map to the same value.
  • entrySet: The Set of key-value pairs contained in the Map. The Map interface provides a small nested interface called Map.Entry that is the type of the elements in this Set.

The Collection-views provide the only means to iterate over a Map.

Iterating over the keys in a Map

Here's an example illustrating the standard idiom for iterating over the keys in a Map:

for (Iterator i=m.keySet().iterator(); i.hasNext(); )
System.out.println(i.next());


Iterating over the values in Map

The idiom for iterating over values is analogous.

for (Iterator i=m.values().iterator(); i.hasNext(); )
System.out.println(i.next());


Iterating over the key value pairs

 

Here's the idiom for iterating over key-value pairs:

for (Iterator i=m.entrySet().iterator(); i.hasNext(); ) {
Map.Entry e = (Map.Entry) i.next();
System.out.println(e.getKey() +
": " + e.getValue());
}

When first presented with these idioms, many people worry that they may be slow because the Map has to create a new Collection object each time a Collection-view operation is called. Rest easy: This is not the case. There's no reason that a Map can't always return the same object each time it is asked for a given Collection-view. This is precisely what all of the JDK's Map implementations do.
With all three Collection-views, calling an Iterator's remove operation removes the associated entry from the backing Map (assuming the Map supports element removal to begin with). With the entrySet view, it is also possible to change the value associated with a key, by calling a Map.Entry's setValue method during iteration (again, assuming the Map supports value modification to begin with). Note that these are the only safe ways to modify a Map during iteration; the behavior is unspecified if the underlying Map is modified in any other way while the iteration is in progress.
The Collection-views support element removal in all its many forms: the remove, removeAll, retainAll, and clear operations, as well as the Iterator.remove operation. (Yet again, this assumes that the backing Map supports element removal.)
The Collection-views do not support element addition under any circumstances. It would make no sense for the keySet and values views, and it's unnecessary for the entrySet view, as the backing Map's put and putAll provide the same functionality.


Tuesday, 21 September 2010

Iterating over map in java

There is no direct iteration over Maps. To iterate over map, get Set of keys or key-value pairs from Map. Maps do not provide an iterator() method as do Lists and Sets. A Set of either keys (keySet()) or key-value Map.Entry elements (entrySet()) can be obtained from the Map, and one can iterate over that.

Order of map elements in iteration

The order of the elements obtained from a Map depends on the type of Map they came from.
TreeMap
The elements are ordered by key.
HashMap
The elements will be in an unpredictable, "chaotic", order.
LinkedHashMap
The elements will be ordered by entry order or last reference order, depending on the type of LinkedHashMap.

Example - Iterating over the "entry set"

This example utility method prints the key-value pairs in a map. This utility method is designed to work for all Maps, so it isn't able to take advantage of generics.
//=====utility method dumpMap
public static void dumpMap(Map mp) {
Iterator it = mp.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry)it.next();
System.out.println(pairs.getKey() +  
                         " = " + pairs.getValue());
}
}