Last active
December 16, 2015 21:39
-
-
Save sivabudh/5501560 to your computer and use it in GitHub Desktop.
An example of how to traverse a map.
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
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