Tuesday, May 20, 2008

Iterating over Map's

A lot of times, i have come across the following code for iterating over a Map

Map values=new HashMap();
Setkeys=values.keySet();
for(Integer key:keys){
String val=values.get(key);
}
A better way is
Map values=new HashMap();
Setkeys=values.keySet();
for(Map.Entryentry:values.entrySet()){
String val=entry.getValue();
}
It is better to get a reference to Map.Entry and then iterate instead of getting all the keys and making the Map object do the work of fetching the value each time

The emperor and me beaching

The Devil next door

Kaiser The Emperor