转自:http://dict.xsoftlab.net/dict/the-use-of-map-and-java-traversal ,谢谢
Map的使用与遍历
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
34
35
36
37
38
|
package net.xsoftlab.dict; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; public class TextMap { public static void main(String[] args) { // 创建Map Map<String, String> map = new HashMap<String, String>(); // 插入元素 map.put( "key1" , "value1" ); map.put( "key2" , "value2" ); // 移除元素 map.remove( "key1" ); // 清空map map.clear(); map.put( "key3" , "value3" ); map.put( "key4" , "value4" ); // 遍历map 方法1 Set<String> set = map.keySet(); Iterator<String> iterator = set.iterator(); while (iterator.hasNext()) { String str = iterator.next(); System.out.println(str + "/" + map.get(str)); } // 遍历map 方法2 for (Map.Entry<String, String> entry : map.entrySet()) { System.out.println(entry.getKey() + "--->" + entry.getValue()); } } } |