怀旧网个人博客网站地址:怀旧网,博客详情:Java Map 介绍以及子类介绍
Java Map 介绍以及子类介绍
Map 常见的API
Map是双列集合的顶层接口,它的功能是全部双列集合都可以继承使用的。
方法使用测试
创建实例化对象
因为Map是接口对象,所以这边创建它的实现类HashMap
Map<String, Integer> map = new HashMap<>();
put 方法
Map<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map);
这边的put添加方法有返回值,可以接受看看输出什么
Integer value1 = map.put("a", 1);
System.out.println(value1);
输出为空
在测试一个
Map<String, Integer> map = new HashMap<>();
Integer value1 = map.put("a", 1);
System.out.println(value1);
map.put("b", 2);
map.put("c", 3);
Integer value2 = map.put("a", 4);
System.out.println(value2);
System.out.println(map);
根据输出结果讨论得出以下结论;
- 通过第一行输出为null说明当执行put方法的时候当前的key没有在数据组里面的时候(没有冲突)就会默认返回null
- 通过第二行的输出,可以看出,这边是执行覆盖操作的时候的返回值,输出为1,代表了原始数据的值被覆盖了,并且将原始的数据做了返回。
- 第三行输出,当前的a值已经被后面的代码修改为4了,说明map和set集合是有区别的,set集合当发现相同数据时,是直接不执行操作,然后map系列是会将原始的值进行覆盖。
remove 方法
remove方法是通过传入的key来删除这一对数据,并且将删除成功的key存在的value返回-测试:
Map<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
Integer res = map.remove("a");
System.out.println(res);
System.out.println(map);
Integer res = map.remove("d");
System.out.println(res);
当删除不存在的数据时,会返回null,代表删除失败!
clear 方法
clear 见名知意就是清空当前map中的所有数据
Map<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
map.clear();
System.out.println(map);
containsKey 和 containsValue 方法
containsKey 是用来判断当前的key是否存在 ----containsValue 是用来判断当前的value是否存在返回值为boolean
Map<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
map.put("d", 3);
boolean keyRes1 = map.containsKey("a");
System.out.println(keyRes1);
boolean keyRes2 = map.containsKey("e");
System.out.println(keyRes2);
boolean valueRes1 = map.containsValue(1);
System.out.println(valueRes1);
boolean valueRes2 = map.containsValue(0);
System.out.println(valueRes2);
isEmpty 方法
用来判断当前map是否为空-返回值为boolean类型(为空返回true,不为空返回false)
Map<String, Integer> map = new HashMap<>();
boolean empty1 = map.isEmpty();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
map.put("d", 3);
boolean empty2 = map.isEmpty();
System.out.println(empty1);
System.out.println(empty2);
size 方法
得到当前map中有多少条记录
Map<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
map.put("d", 3);
int size = map.size();
System.out.println(size);
Map 的遍历方式
一共有如下三中标遍历方式
- 键找值
- 键值对
- Lambda表达式
键找值 方式遍历
原理就是:先获取所有的key的Set集合,然后在通过每一个key去找到对应的值即可
- 获取当前所有的key(类型为Set)
Set<String> keySet = map.keySet();
- 当获取到所有的Set后,就直接可以使用对应的Set的遍历方式获取到每一个key
Map<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
map.put("d", 3);
Set<String> keySet = map.keySet();
for (String key : keySet) {
System.out.println(key);
}
- 最后通过键获取值,并打印(通过key获取value可以使用, get(key) 方法来获取)
Map<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("d", 3);
map.put("c", 3);
map.put("b", 2);
Set<String> keySet = map.keySet();
for (String key : keySet) {
System.out.println(key + "-->" + map.get(key));
}
成功获取到所有数据-同时可以从输出内容看出,map的存取也是无序的
键值对 方式遍历
首先获取到map中的所有键值对数据-(可以通过entrySet() 方法)
当获取到数据后,直接使用增强for就可以直接进行遍历
Map<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("d", 3);
map.put("c", 3);
map.put("b", 2);
Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
for (Map.Entry<String, Integer> entry : entrySet) {
System.out.println(entry);
}
绘制也可以通过下面的方法,将键和值分别取出来遍历
Map<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("d", 3);
map.put("c", 3);
map.put("b", 2);
Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
for (Map.Entry<String, Integer> entry : entrySet) {
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println(key + "-->" + value);
}
Lambda 方式遍历
这边的底层则是采用获取键值对数据的方式,然后在采用增强fro循环遍历
Map<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("d", 3);
map.put("c", 3);
map.put("b", 2);
map.forEach(new BiConsumer<String, Integer>() {
@Override
public void accept(String key, Integer value) {
System.out.println(key + "-->" + value);
}
});
简化写法
Map<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("d", 3);
map.put("c", 3);
map.put("b", 2);
map.forEach((key, value) -> System.out.println(key + "-->" + value));
HashMap 介绍
特点
总结
LinkedHashMap 介绍
特点
可以参考前面的LinkedHashSet:网页链接 在最底下的位置
代码测试
Map<String, Integer> linkedHashMap = new LinkedHashMap<>();
linkedHashMap.put("a", 1);
linkedHashMap.put("d", 3);
linkedHashMap.put("c", 3);
linkedHashMap.put("b", 2);
System.out.println(linkedHashMap);
输出结果顺序和添加顺序一致
TreeMap 介绍
特点
代码编写时-自定义类的两种排序规则
代码演示
- 代码演示可以参考前面的TreeSet代码: 网页链接