Tuesday, July 15, 2008

HashMap loop in java

This is how you loop through the Key Value pairs in a Hashmap

Iterator iter = values.keySet().iterator();

while (iter.hasNext()) {
String key = iter.next();
String value = values.get(key);
System.out.println("Key ="+key+"\n Value"+value)
}

Here, I have implicitly assumed that values is a HashMap but as you know it can be of any Object that you like. I just used it for string and felt it might be a good example if someone is looking for it