Java iterate through map, hashmap - working source code

Iterating through Map in Java - working efficient source code 

Map<String, Object> map = ...;

The solution uses map.keySet(), map.values(), and map.entrySet().

If you're only interested in the keys, you can iterate through the keySet() of the map:
for (String key : map.keySet()) {
    // ...
}

If you only need the values, use values():
for (Object value : map.values()) {
    // ...
}

Finally, if you want both the key and value, use entrySet():
for (Map.Entry<String, Object> entry : map.entrySet()) {
    String key = entry.getKey();
    Object value = entry.getValue();
    // ...
}




No comments :

Post a Comment

Your Comment and Question will help to make this blog better...