Java中Map集合体系的基本使用和常用API是什么
导读:本文共4279字符,通常情况下阅读需要14分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要: Map集合概述和使用Map集合是一种双列集合,每个元素包含两个数据。Map集合的每个元素的格式:key=value(键值对元素)。Map集合也被称为“键值对集合”。Map集合整体格式:Collection集合的格式: [元素1,元素2,元素3..]M... ...
目录
(为您整理了一些要点),点击可以直达。
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体系一致)
publicstaticvoidmain(String[]args){//创建一个HashMap对象Map<String,Integer>maps=newHashMap<>();//向集合添加元素maps.put("桌子",2);maps.put("凳子",10);maps.put("桌子",10);//键一样会覆盖前面的maps.put(null,null);//键值对可以为null //输出集合,可以发现是无序的System.out.println(maps);//{null=null,凳子=10,桌子=10}}LinkedHashMap:元素按照键是有序,不重复,无索引,值不做要求。
publicstaticvoidmain(String[]args){//创建一个LinkedHashMap对象//Map<String,Integer>maps=newHashMap<>();Map<String,Integer>maps=newLinkedHashMap<>();//向集合添加元素maps.put("桌子",2);maps.put("凳子",10);maps.put("桌子",10);//键一样会覆盖前面的maps.put(null,null);//键值对可以为null//输出集合,是有序的System.out.println(maps);//{桌子=10,凳子=10,null=null}}TreeMap:元素是按照键排序,不重复,无索引的,值不做要求。
publicstaticvoidmain(String[]args){//创建一个HashMap对象//Map<String,Integer>maps=newHashMap<>();//Map<String,Integer>maps=newLinkedHashMap<>();Map<String,Integer>maps=newTreeMap<>();//向集合添加元素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方法添加元素
publicstaticvoidmain(String[]args){//创建Map集合对象Map<String,Integer>maps=newHashMap<>();//添加元素maps.put("华为",10);maps.put("小米",5);maps.put("iPhone",6);maps.put("生活用品",15);System.out.println(maps);//{iPhone=6,生活用品=15,华为=10,小米=5}}
remove方法, 根据键删除元素
publicstaticvoidmain(String[]args){//创建Map集合对象Map<String,Integer>maps=newHashMap<>();//添加元素maps.put("华为",10);maps.put("小米",5);maps.put("iPhone",6);maps.put("生活用品",15);//删除元素maps.remove("小米");System.out.println(maps);//{iPhone=6,生活用品=15,华为=10}}
clear方法, 清空集合元素
publicstaticvoidmain(String[]args){//创建Map集合对象Map<String,Integer>maps=newHashMap<>();//添加元素maps.put("华为",10);maps.put("小米",5);maps.put("iPhone",6);maps.put("生活用品",15);//清空元素maps.clear();System.out.println(maps);//{}}
containsKey()方法, 判断是否包含指定键
publicstaticvoidmain(String[]args){//创建Map集合对象Map<String,Integer>maps=newHashMap<>();//添加元素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方法, 判断是否包含指定值
publicstaticvoidmain(String[]args){//创建Map集合对象Map<String,Integer>maps=newHashMap<>();//添加元素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, 判断集合是否为空
publicstaticvoidmain(String[]args){//创建Map集合对象Map<String,Integer>maps=newHashMap<>();//添加元素maps.put("华为",10);maps.put("小米",5);maps.put("iPhone",6);maps.put("生活用品",15);//判断集合是否为空System.out.println(maps.isEmpty());//false}
size方法, 集合元素的个数
publicstaticvoidmain(String[]args){//创建Map集合对象Map<String,Integer>maps=newHashMap<>();//添加元素maps.put("华为",10);maps.put("小米",5);maps.put("iPhone",6);maps.put("生活用品",15);//返回集合元素的个数System.out.println(maps.size());//4}
扩展方法: putAll合并其他集合, 合并遇到重复的key会进行合并
publicstaticvoidmain(String[]args){Map<String,Integer>map1=newHashMap<>();map1.put("java",1);map1.put("C语言",2);Map<String,Integer>map2=newHashMap<>();map2.put("python",4);map2.put("linux",7);//合并两个集合map1.putAll(map2);System.out.println(map1);//{{python=4,java=7,C语言=2}}
</div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
Java中Map集合体系的基本使用和常用API是什么的详细内容,希望对您有所帮助,信息来源于网络。