复制代码
1
2
3
4
5
6Map 查找表 java.util.Map接口,规定了Map的相关功能。 Map体现的结构是一个多行两列的表格,其中左列称为key,右列称为value Map总是根据key获取value. 常用的实现类: java.util.HashMap散列表,当今查询速度最快的数据结
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
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83public class MapDemo { public static void main(String[] args) { Map<String,Integer> map = new HashMap<>(); //LinkedHashMap是有序的散列表,遍历顺序与put顺序一致。 // Map<String,Integer> map = new LinkedHashMap<>(); /* V put(K k ,V v) 将一组键值对存入到Map中。 Map要求key是不允许重复的!!!!!!!!!!!!key的equals不能为true 因此,使用重复的key存入新的value时,那么就是替换value操作。此时put方法 返回值为被替换的value。否则返回值为null。 */ Integer value = map.put("语文",99);//返回值为null,没有value被替换 System.out.println(value);//null map.put("数学",98); map.put("英语",97); map.put("物理",96); map.put("化学",99); System.out.println(map); value = map.put("英语",60);//替换英语原来的值97 System.out.println(value);//97 System.out.println(map); /* V get(Object key) 根据给定的key获取对应的value。 如果给定的key不存在则返回值为null。 */ value = map.get("数学"); System.out.println(value); value = map.get("体育"); /* 如果Map的value是包装类类型,当我们获取value值时,也一定使用包装类 类型的变量去接收返回值。避免使用基本类型。因为这可能导致触发自动拆箱 特性。如果返回值为null导致自动拆箱出现空指针异常 int value1 = map.get("体育"); VVV 自动拆箱 int value1 = map.get("体育").intValue(); null.intValue(); 空指针! */ System.out.println(value);//null /* int size() 返回当前Map的元素个数,每一组键值对算是一个元素 */ int size = map.size(); System.out.println("size:"+size); // map.clear();//清空Map /* boolean containsKey(Object key) 判断当前Map是否包含给定的key boolean containsValue(Value value) 判断当前Map是否包含给定的value */ // boolean ck = map.containsKey("化学"); boolean ck = map.containsKey("体育"); System.out.println("包含key:"+ck); boolean cv = map.containsValue(98); System.out.println("包含value:"+cv); System.out.println(map); /* V remove(Object key) 从Map中删除给定的key所对应的键值对。返回值为这个key对应的value 如果给定的key在Map中不存在,则没有任何效果。返回值也为null。 */ value = map.remove("数学"); System.out.println(value); System.out.println(map); } }
Map的遍历 Map提供了三种遍历方式:
1:遍历所有的key
2:遍历每一组键值对
3:遍历所有的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
48public class MapDemo2 { public static void main(String[] args) { Map<String,Integer> map = new HashMap<>(); map.put("语文",99); map.put("数学",98); map.put("英语",97); map.put("物理",96); map.put("化学",99); System.out.println(map); /* 遍历所有的key Set keySet() 将当前Map中所有的key以一个Set集合形式返回。 遍历该集合等于遍历了所有的key */ Set<String> keySet = map.keySet(); for(String key : keySet){//新循环 System.out.println("key:"+key); } /* 遍历每一组键值对 Set entrySet() 将Map中每一组键值对以一个Entry实例表示,并将所有的Entry实例存入Set 集合后将其返回 Entry是Map的内部接口,每一个Entry实例应当表示Map中的一组键值对。 */ Set<Entry<String,Integer>> entrySet = map.entrySet(); for(Entry<String,Integer> entry: entrySet){ String key = entry.getKey(); Integer value = entry.getValue(); System.out.println(key+":"+value); } /* 遍历所有的value Collection values() 该方法会将当前Map中所有的value以一个集合形式返回。 */ Collection<Integer> values = map.values(); for(Integer value : values){ System.out.println("value:"+value); } } }
复制代码
1JDK8之后,集合与Map都提供了一个方法forEach(),可以基于lambda表达式遍历
复制代码
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
51public class MapDemo3 { public static void main(String[] args) { Collection<String> c = new ArrayList<>(); c.add("one"); c.add("two"); c.add("three"); c.add("four"); c.add("five"); System.out.println(c); /* forEach方法中传入的lambda表达式会被调用若干次(取决于集合的元素个数), 每调用一次lambda,参数e表示集合中的一个元素。 */ // c.forEach((e)->{ // System.out.println(e); // }); // Consumer<String> con = new Consumer<String>() { // public void accept(String e) { // System.out.println(e); // } // }; // c.forEach(con); // c.forEach(e->System.out.println(e)); /* 如果lambda表达式中参数正好用于方法体调用的方法所使用的参数 例如下面的代码: lambda表达式参数e正好被用于lambda表达式方法体中的代码: System.out.println(e)中的参数 此时可以使用lambda表达式的变种写法:方法引用 System.out::println */ // c.forEach(e->System.out.println(e)); c.forEach(System.out::println); Map<String,Integer> map = new HashMap<>(); map.put("语文",99); map.put("数学",98); map.put("英语",97); map.put("物理",96); map.put("化学",99); map.forEach((k,v)->System.out.println(k+":"+v)); } }
最后
以上就是无辜黑夜最近收集整理的关于JAVA中的Map查找表的全部内容,更多相关JAVA中内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复