Skip to content

Instantly share code, notes, and snippets.

@tolinwei
Created November 4, 2016 23:04
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 tolinwei/99cf8704c1396c94783006e1b6a29150 to your computer and use it in GitHub Desktop.
Save tolinwei/99cf8704c1396c94783006e1b6a29150 to your computer and use it in GitHub Desktop.
import java.util.*;
public class Main {
public static void main(String args[])
{
Map<Integer, Integer> map = new HashMap();
map.put(1, 3);
map.put(2, 4);
map.put(5, 7);
/* Method 1, to use entrySet() */
/*
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
}
*/
/* Method 2, to use keySet() and valueSet() */
/* Method 3, iterator */
Iterator it = map.entrySet().iterator(); // Set has iterator, but HashMap doesn't
while (it.hasNext()) {
Map.Entry<Integer, Integer> entry = (Map.Entry)it.next(); // Iterator要强制转换
System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment