Skip to content

Instantly share code, notes, and snippets.

@sivabudh
Last active December 16, 2015 21:39
Show Gist options
  • Save sivabudh/5501560 to your computer and use it in GitHub Desktop.
Save sivabudh/5501560 to your computer and use it in GitHub Desktop.
An example of how to traverse a map.
public static void main(String[]args){
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(5, "pac");
map.put(6, "got");
//
// Iterator
//
System.out.println("Iterator way");
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry)it.next();
System.out.println(pairs.getKey() + " " + pairs.getValue());
}
//
// For loop
//
System.out.println("For-loop way");
for(Integer key : map.keySet()) {
System.out.println(key + " " + map.get(key));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment