1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class MapTest {
public
static void main(String args[]) {
Map<Integer, String> map = new
HashMap<Integer, String>();
for
( int i = 0 ; i < 2000 ; i++) {
map.put(i, i * 10
+ "" );
}
// 第一种:通过Map.keySet()遍历key和value
// 这里有一个二次取值的过程,所以并不推荐
for
(Integer key : map.keySet()) {
System.out.println( "key = "
+ key + " and value = "
+ map.get(key));
}
// 第二种:通过Map.entrySet()使用iterator()遍历key和value
Iterator<Map.Entry<Integer, String>> iterator = map.entrySet().iterator();
while
(iterator.hasNext()) {
Map.Entry<Integer, String> entry = iterator.next();
System.out.println( "key = "
+ entry.getKey() + " and value = "
+ entry.getValue());
}
// 第三种:通过Map.entrySet()遍历key和value
// JDK1.5后的遍历形式,代码看起来比较整洁,更推荐采用
for
(Map.Entry<Integer, String> entry : map.entrySet()) {
System.out.println( "key = "
+ entry.getKey() + " and value = "
+ entry.getValue());
}
}
} |