一、Map<String,Object>
String:key的类型
Object:value的类型,value可能是String,或者int类型,什么类型都可以
对于Map接口来说,本身不能直接迭代输出,因为Map的每个位置存放的是一对值(key,value),迭代每次只能输出一个值
需要先取到key的集合,再根据key迭代输出value
复制代码
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67package com.xf.collection; import org.junit.Test; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; public class MyMap { @Test public void method1() { Map<String, Object> map = new HashMap<>(); map.put("001", "hello"); map.put("002", "world"); map.put("003", "main"); //方法1 keySet集合迭代 Set<String> keyset = map.keySet(); Iterator<String> it = keyset.iterator(); while (it.hasNext()) { Object key = it.next(); System.out.println(key + "=" + map.get(key)); } } @Test public void method2() { Map<String, Object> map = new HashMap<>(); map.put("001", "hello"); map.put("002", "world"); map.put("003", "main"); //方法2 entrySet集合迭代 Set<Map.Entry<String, Object>> entrySet = map.entrySet(); Iterator<Map.Entry<String, Object>> it = entrySet.iterator(); while (it.hasNext()) { Map.Entry<String, Object> entry = it.next(); System.out.println(entry.getKey() + "=" + entry.getValue()); } } @Test public void method3() { Map<String, Object> map = new HashMap<>(); map.put("001", "hello"); map.put("002", "world"); map.put("003", "main"); //方法3 keySet集合for-each循环 for (String key : map.keySet()) { System.out.println(key + "=" + map.get(key)); } } @Test public void method4() { Map<String, Object> map = new HashMap<>(); map.put("001", "hello"); map.put("002", "world"); map.put("003", "main"); //方法4 entrySet集合for-each循环 for (Map.Entry<String, Object> entry : map.entrySet()) { System.out.println(entry.getKey() + "=" + entry.getValue()); } } }
四种方式中,method1 和 method2 是通过迭代器来显示完成的,method3 和 method4 是通过for-each来隐式的通过迭代器来完成的。
同时 method1 和 method3 是通过key的集合来完成的,method2 和 method4 是通过entry 的集合来完成的。
方法1 和方法2 的区别
一个是获取keySet ,一个是获取entrySet
推荐使用entrySet 的方式去获取,查看map通过key获取value的方法
复制代码
1
2
3
4
5
6
7public V get(Object key) { if (key == null) return getForNullKey(); Entry<K,V> entry = getEntry(key); return null == entry ? null : entry.getValue(); }
也是先获取该key对应的entry,然后再获取value值,所以,推荐使用entrySet 方法,再遍历entry集合的方式来遍历map
map 接口还有一个方法 values() ,由于仅能取到所有的value值,而取不到key值,所以在这里就算不上遍历map了,只能算上遍历map的value值。
扩展:
复制代码
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@Test public void method5() { //遍历Map <String, ArrayList> map = new HashMap <String,ArrayList>(); Map<String, ArrayList> map = new HashMap<String, ArrayList>(); Set<String> keys = map.keySet(); Iterator<String> iterator = keys.iterator(); while (iterator.hasNext()) { String key = iterator.next(); ArrayList arrayList = map.get(key); for (Object o : arrayList) { System.out.println(o); } } } @Test public void method6() { //遍历Map Map<String, List> map1 = new HashMap<String, List>(); Map<String, List> map1 = new HashMap<String, List>(); for (Map.Entry entry : map1.entrySet()) { String key = entry.getKey().toString(); List<String> list = (List) entry.getValue(); for (String value : list) { System.out.println(key + "====" + value); } } }
最后
以上就是纯真汽车最近收集整理的关于map的四种遍历方式方法1 和方法2 的区别的全部内容,更多相关map的四种遍历方式方法1内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复