Java中Map集合体系的基本使用和常用API
创始人
2024-03-23 08:39:40

文章目录

  • Map集合体系
    • Map集合概述和使用
    • Map集合体系的特点
    • Map集合常用的API

Map集合体系

Map集合概述和使用

Map集合是一种双列集合,每个元素包含两个数据。

Map集合的每个元素的格式:key=value(键值对元素)。

Map集合也被称为“键值对集合”。

Map集合整体格式:

Collection集合的格式: [元素1,元素2,元素3..]

Map集合的完整格式:{key1=value1 , key2=value2 , key3=value3 , ...}

Map集合的使用场景之一:购物车系统

分析:

购物车提供的四个商品和购买的数量在后台需要容器存储。

每个商品对象都一一对应一个购买数量。

把商品对象看成是Map集合的建,购买数量看成Map集合的值。

例如: {商品1=2 , 商品2=3 , 商品3 = 2 , 商品4= 3}

在这里插入图片描述

Map集合体系的特点

在这里插入图片描述

Map集合中使用最多的Map集合是HashMap。

重点掌握HashMap , LinkedHashMap , TreeMap。其他的后续理解。

Map集合体系特点:

Map集合的特点都是由键决定的。

Map集合的键是无序,不重复的,无索引的,值不做要求(可以重复)。

Map集合后面重复的键对应的值会覆盖前面重复键的值。

Map集合的键值对都可以为null。

Map集合实现类特点:

HashMap:元素按照键是无序,不重复,无索引,值不做要求。(与Map体系一致)

public static void main(String[] args) {// 创建一个HashMap对象Map maps = new HashMap<>();// 向集合添加元素maps.put("桌子", 2);maps.put("凳子", 10);maps.put("桌子", 10); // 键一样会覆盖前面的maps.put(null, null); // 键值对可以为null// 输出集合, 可以发现是无序的System.out.println(maps); // {null=null, 凳子=10, 桌子=10}
}

LinkedHashMap:元素按照键是有序,不重复,无索引,值不做要求。

public static void main(String[] args) {// 创建一个LinkedHashMap对象// Map maps = new HashMap<>();Map maps = new LinkedHashMap<>();// 向集合添加元素maps.put("桌子", 2);maps.put("凳子", 10);maps.put("桌子", 10); // 键一样会覆盖前面的maps.put(null, null); // 键值对可以为null// 输出集合, 是有序的System.out.println(maps); // {桌子=10, 凳子=10, null=null}
}

TreeMap:元素是按照键排序,不重复,无索引的,值不做要求。

public static void main(String[] args) {// 创建一个HashMap对象// Map maps = new HashMap<>();// Map maps = new LinkedHashMap<>();Map maps = new TreeMap<>();// 向集合添加元素maps.put("ddd", 2);maps.put("bbb", 10);maps.put("ddd", 3);maps.put("aaa", 5);maps.put("ccc", 1);// 输出集合, 元素按照键进行排序System.out.println(maps); // {aaa=5, bbb=10, ccc=1, ddd=3}
}

Map集合常用的API

Map集合:

Map是双列集合的祖宗接口,它的功能是全部双列集合都可以继承使用的。

Map API如下:

方法名称说明
put(K key,V value)添加元素
remove(Object key)根据键, 删除键值对元素
clear()移除所有的键值对元素
containsKey(Object key)判断集合是否包含指定的键
containsValue(Object value)判断集合是否包含指定的值
isEmpty()判断集合是否为空
size()集合的长度,也就是集合中键值对的个数

put方法添加元素

public static void main(String[] args) {// 创建Map集合对象Map maps = new HashMap<>();// 添加元素maps.put("华为", 10);maps.put("小米", 5);maps.put("iPhone", 6);maps.put("生活用品", 15);System.out.println(maps); // {iPhone=6, 生活用品=15, 华为=10, 小米=5}
}

remove方法, 根据键删除元素

public static void main(String[] args) {// 创建Map集合对象Map maps = new HashMap<>();// 添加元素maps.put("华为", 10);maps.put("小米", 5);maps.put("iPhone", 6);maps.put("生活用品", 15);// 删除元素maps.remove("小米");System.out.println(maps); // {iPhone=6, 生活用品=15, 华为=10}
}

clear方法, 清空集合元素

public static void main(String[] args) {// 创建Map集合对象Map maps = new HashMap<>();// 添加元素maps.put("华为", 10);maps.put("小米", 5);maps.put("iPhone", 6);maps.put("生活用品", 15);// 清空元素maps.clear();System.out.println(maps); // {}
}

containsKey()方法, 判断是否包含指定键

public static void main(String[] args) {// 创建Map集合对象Map maps = new HashMap<>();// 添加元素maps.put("华为", 10);maps.put("小米", 5);maps.put("iPhone", 6);maps.put("生活用品", 15);// 判断是否包含指定键System.out.println(maps.containsKey("华为")); // trueSystem.out.println(maps.containsKey("魅族")); // false
}

containsValue方法, 判断是否包含指定值

public static void main(String[] args) {// 创建Map集合对象Map maps = new HashMap<>();// 添加元素maps.put("华为", 10);maps.put("小米", 5);maps.put("iPhone", 6);maps.put("生活用品", 15);// 判断是否包含指定值System.out.println(maps.containsValue(6)); // trueSystem.out.println(maps.containsValue(99)); // false
}

isEmpty, 判断集合是否为空

public static void main(String[] args) {// 创建Map集合对象Map maps = new HashMap<>();// 添加元素maps.put("华为", 10);maps.put("小米", 5);maps.put("iPhone", 6);maps.put("生活用品", 15);// 判断集合是否为空System.out.println(maps.isEmpty()); // false
}

size方法, 集合元素的个数

public static void main(String[] args) {// 创建Map集合对象Map maps = new HashMap<>();// 添加元素maps.put("华为", 10);maps.put("小米", 5);maps.put("iPhone", 6);maps.put("生活用品", 15);// 返回集合元素的个数System.out.println(maps.size()); // 4
}

扩展方法: putAll合并其他集合, 合并遇到重复的key会进行合并

public static void main(String[] args) {Map map1 = new HashMap<>();map1.put("java", 1);map1.put("C语言", 2);Map map2 = new HashMap<>();map2.put("python", 4);map2.put("linux", 7);// 合并两个集合map1.putAll(map2);System.out.println(map1); // {{python=4, java=7, C语言=2}
}

相关内容

热门资讯

告别语言壁垒!BLINBLIN... 当你还在为跨国会议上的语言障碍焦头烂额,为出国旅行时的沟通不畅手足无措,为外语学习时的发音不准苦恼不...
金观平:因地制宜推进节能降碳 近期召开的国务院常务会议对节能降碳工作作出重要部署,明确提出要更高水平更高质量做好节能降碳工作,加大...
2025回顾 | 2025年青... 2025年青岛改扩建高速公路近260公里建设规模创历史之最;1个重点项目建成通车,2个重点项目计划2...
委内瑞拉国民警卫队称已在全国范... 转自:北京日报客户端当地时间6日,委内瑞拉国民警卫队称,已在全国范围内部署兵力,同时,国家警察局也在...
刘旭:电力危机折射美国深层治理... 前段时间,在旧金山一场人工智能(AI)会议上,全美最大公用事业公司之一爱克斯龙的首席执行官巴特勒将美...