Wednesday, December 3, 2008

Traversing the Map (HashMap) in Java

This is how to loop over a map in java similar to PHP's foreach statement. There are, of course, other methods using iterator and entrySet, but who needs them when you have this:
//HashMap<Key, Value> map = new HashMap<Key, Value>();
for (Key key : map.keySet()) {
Value value = map.get(key);
}

No comments: