Skip to content

Instantly share code, notes, and snippets.

@hoandang
Last active December 13, 2015 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hoandang/4960784 to your computer and use it in GitHub Desktop.
Save hoandang/4960784 to your computer and use it in GitHub Desktop.
Three ways to iterate a HashMap in Java
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