Just now, I was going through Solr code and came upon a snippet of code to iterate over a Map, which I did not know before:

Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "one");
map.put(2, "two");

for (Map.Entry<Integer, String> e : map.entrySet())  {
  System.out.println(e.getKey()); //prints 2 and 1
  System.out.println(e.getValue()); //prints two and one
}

API for the Entry inner class.

Advertisements