Last active
December 13, 2015 19:09
-
-
Save hoandang/4960784 to your computer and use it in GitHub Desktop.
Three ways to iterate a HashMap in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Map<String, Object> map = ...; | |
//If only interested in the keys, you can iterate through the keySet() of the map: | |
for (String key : map.keySet()) { | |
// ... | |
} | |
//If only need the values, use values(): | |
for (Object value : map.values()) { | |
// ... | |
} | |
//Finally, if want both the key and value, use entrySet(): | |
for (Map.Entry<String, Object> entry : map.entrySet()) { | |
String key = entry.getKey(); | |
Object value = entry.getValue(); | |
// ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment