Logo

Programming-Idioms

History of Idiom 13 > diff from v20 to v21

Edit summary for version 21 by :

Version 20

2015-09-13, 12:15:32

Version 21

2015-09-13, 12:15:50

Idiom #13 Iterate over map keys and values

Print each key k with its value x from an associative array mymap

Idiom #13 Iterate over map keys and values

Print each key k with its value x from an associative array mymap

Code
for (Map.Entry<Object, Object> entry : mymap.entrySet()) {
    Object k = entry.getKey();
    Object x = entry.getValue();
    System.out.println("Key=" + k + ", Value=" + x);
}
Code
for (Map.Entry<Object, Object> entry : mymap.entrySet()) {
    Object k = entry.getKey();
    Object x = entry.getValue();
    System.out.println("Key=" + k + ", Value=" + x);
}
Comments bubble
Instead of Object, prefer using sensible key type and value type.
Comments bubble
Instead of Object, prefer using sensible key type and value type for your map.
Origin
http://stackoverflow.com/questions/1066589/java-iterate-through-hashmap#answer-1066607
Origin
http://stackoverflow.com/questions/1066589/java-iterate-through-hashmap#answer-1066607