一、Map特点

1.Map集合概述: Map集合是双列集合,每个元素拥有两个数据 Map集合每个元素的格式为:key=value(键值对集合),因此也可以称作键值对集合

2.Map集合体系特点: Map集合的特点由键决定 Map集合的键是无序,不重复,无索引的,值可重复,且可以为null Map集合后面重复的键对应的值会覆盖前面重复键的值

Map map = new HashMap<>();

map.put("a", 2);

map.put("b", 1);

map.put("c", 1);

map.put("d", null);

System.out.println(map); // 输出结果为 {a=2, b=1, c=1, d=null}

3.Map集合实现类的特点: HashMap:与Map体系是完全一致的 LinkedHashMap:键是有序的,不重复,无索引,值可重复 TreeMap:键的位置是通过排序确定的,且不重复,无索引,值可重复

二、Map集合常用方法

1.put(K key, V value);

添加元素

Map map = new HashMap<>();

map.put("a", 2);

map.put("b", 1);

map.put("c", 1);

map.put("d", null);

System.out.println(map); // 输出结果为 {a=2, b=1, c=1, d=null}

2.clear();

清空元素

map.clear();

System.out.print(map); // 输出结果为{}

3.isEmpty();

判断集合是否为空

System.out.print(map.isEmpty()); // 输出结果为 true

4.get(Object key);

取得该键所对应的值

Integer key1 = map.get("a");

Integer key2 = map.get("ad");

System.out.println(key1); // 输出结果为 2

System.out.println(key2); // 输出结果为 null

注意:若该集合内没有所输入的键,则会返回null

5.remove(Object key);

根据键删除整个元素,并返回此元素的值

Integer value1 = map.remove("b");

System.out.println(value1); // 输出结果为 1

System.out.println(map); // 输出结果为 {a=2, c=1, d=null}

6.containsKey(Object key)/containsValue(Object value);

检查该集合是否包含给定键/值,并返回一个Boolean值

System.out.println(map.containsKey("a")); // 输出结果为 true

System.out.println(map.containsKey("ad")); // 输出结果为 false

System.out.println(map.containsValue(2)); // 输出结果为 true

System.out.println(map.containsValue("2")); // 输出结果为 false

7.keySet()/values();

获取该Map的 键/值 集合

Set keys = map.keySet();

System.out.println(keys); // 输出结果为 [a, c, d]

Collection values = map.values();

System.out.println(values); // 输出结果为 [2, 1, null]

8.map1.putAll(map2);

合并集合

Map map1 = new HashMap<>();

Map map2 = new HashMap<>();

map1.put("ball breaker", 1);

map1.put("scary monster", 2);

map2.put("scary monster", 3);

map2.put("act 4", 4);

map1.putAll(map2);

System.out.println(map1); // 输出结果为 {act 4=4, ball breaker=1, scary monster=3}

System.out.println(map2); // 输出结果为 {act 4=4, scary monster=3}

不难看出:在合并之后map2的元素未发生改变,map1在原先的基础上增加了map2的元素,若有相同的键,还会发生覆盖;同时需要注意的是,将要发生合并的两个集合键值类型需要一一对应。

9.entrySet();

将Map转化为Set。在此过程中,键值对将会封装成一个整体对象,这种键值对类型也称作Map.Entry类型

Map map = new HashMap<>();

map.put("a", 1);

map.put("b", 2);

map.put("c", 3);

Set> set = map.entrySet();

System.out.println(set); // 输出结果为 [a=1, b=2, c=3]

三、Map三种遍历方式

1.根据键来进行遍历

①先利用keySet取得键集合 ②在foreach中,利用get(Object key)取得每个键对应的值

Map map = new HashMap<>();

map.put("a", 1);

map.put("b", 2);

map.put("c", 3);

Set keys = map.keySet();

for(String key : keys){

System.out.println(key + "对应值为:" + map.get(key));

}

2.键值对遍历

①先使用enTrySet()将Map集合转化为Set集合 ②再依次使用getKey()和getValue()取得键和值

Set> sets = map.entrySet();

for(Map.Entry set : sets){

String key = set.getKey();

Integer value = set.getValue();

System.out.println(key + " 和 " + value + "一组");

}

3.结合Lambda表达式遍历

Map map = new HashMap<>();

map.put("a", 1);

map.put("b", 2);

map.put("c", 3);

map.forEach((key, value) ->

{

System.out.println(key + " 对应 " + value);

});

文章链接

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。