Created
November 4, 2016 23:04
-
-
Save tolinwei/99cf8704c1396c94783006e1b6a29150 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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