首页 > 其他分享 >Map简介

Map简介

时间:2023-08-16 10:04:37浏览次数:34  
标签:Map map 简介 System println put out

一、Map特点

1.Map集合概述:

Map集合是双列集合,每个元素拥有两个数据

Map集合每个元素的格式为:key=value(键值对集合),因此也可以称作键值对集合

2.Map集合体系特点:

Map集合的特点由键决定

Map集合的键是无序,不重复,无索引的,值可重复,且可以为null

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

Map<String, Integer> 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<String, Integer> 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 values = map.values();
System.out.println(values); // 输出结果为 [2, 1, null]

8.map1.putAll(map2);

合并集合

Map<String, Integer> map1 = new HashMap<>();
Map<String, Integer> 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<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
Set<Map.Entry<String, Integer>> set = map.entrySet();
System.out.println(set); // 输出结果为 [a=1, b=2, c=3]

三、Map三种遍历方式

1.根据键来进行遍历

①先利用keySet取得键集合

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

Map<String, Integer> 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<Map.Entry<String, Integer>> sets = map.entrySet();
for(Map.Entry<String, Integer> set : sets){
String key = set.getKey();
Integer value = set.getValue();
	System.out.println(key + " 和 " + value + "一组");
}

3.结合Lambda表达式遍历

Map<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
map.forEach((key, value) ->
{
	System.out.println(key + " 对应 " + value);
});

标签:Map,map,简介,System,println,put,out
From: https://blog.51cto.com/u_16124071/7098775

相关文章

  • 执行kubeadm 出现 FATAL: the ConfigMap "kubeadm-config" in the kube-system namesp
    现象: [upgrade/config]Makingsuretheconfigurationiscorrect:[upgrade/config]Readingconfigurationfromthecluster...[upgrade/config]FYI:Youcanlookatthisconfigfilewith'kubectl-nkube-systemgetcmkubeadm-config-oyaml'[upgrade/c......
  • 集合+hashmap
    数组数组(Array)是一种用连续的内存空间存储相同数据类型数据的线性数据结构。面试题:为什么数组索引从0开始?假如从1开始会怎么样?操作数组的时间复杂度当未知数组查询时,时间复杂度为O(n)总结———————————————————————————————————......
  • mybatis系列: 简介以及使用
    目录一、简介二、简单使用一、简介MyBatis本质上就是对JDBC的封装,通过MyBatis完成CRUD。MyBatis在三层架构中负责持久层的,属于持久层框架。MyBatis的发展历程:【引用百度百科】MyBatis本是apache的一个开源项目iBatis,2010年这个项目由apachesoftwarefoundation迁移到了go......
  • 【Python】解决“Tk_GetPixmap: Error from CreateDIBSection”闪退问题
    解决Python使用Tkinter的Notebook切换标签时出现的“Tk_GetPixmap:ErrorfromCreateDIBSection操作成功完成”闪退问题零、问题描述在使用Tkinter的Notebook控件时,对其标签进行切换,发现切换不了,一切换就报如下图错误:第一个页面正常显示,后面的就都不行了,都是报这个错误。第......
  • AI简介-AI基础系列文章第1篇
    您的关注是对我最大的支持......
  • 【校招VIP】java语言考点之ConcurrentHashMap1.7和1.8
    考点介绍:ConcurrentHashMap是JAVA校招面试的热门考点,主要集中在1.7和1.8的底层结构和相关的性能提高。理解这个考点要从map本身的并发问题出发,再到hashTable的低性能并发安全,引申到ConcurrentHashMap的分块处理。同时要理解读锁和写锁的区别一、考点题目1、ConcurrentHashMap与......
  • Mybatis中的resultType和resultMap
    综述MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接返回设置的类型,而resultMap则是对外部ResultMap的引用,但是resultType跟resultMap不能同时存在。在MyBatis进行查询映射时,其实查询出来的每一个属性都是放在一个对应的Map里面......
  • 我的BIOS之行2-Keyboard Controller 简介
    KeyboardController简介主板的键盘有一块专用的接口芯片,一般是采用一块单片微处理器8042(现在大多已集成在南桥或SIO里)。它控制整个键盘的工作,包括加电自检、键盘扫描码的缓冲以及与主板的通讯。INT09H是H/W中断,对应IRQ1,INT16H是一个S/W中断。当键盘的一个键被按下时,键盘接......
  • 我的BIOS之行3-UEFI的简介
    UEFI的世界观如果有人对BIOS有兴趣,那么不得不提UEFI了。当然笔者这边只是代码与说明的搬运工,通过UEFI组织的官方文档来带大家了解UEFI。当然您要是觉得笔者就是个DB,想直接翻UEFI的文档,不想看笔者的瞎逼文章的话,请直接点击这里,自行下载看就是了。UEFI的简介文章开头就开始介绍......
  • 《简介篇》QT是什么
    参考链接:https://blog.csdn.net/m0_65682542/article/details/126731164QT是什么Qt:一个跨平台的C++开发库,主要用来开发图形用户界面程序。名字含义名字含义:字母Q作为所有类的前缀,是因为Haarard写这个字母看起来特别的漂亮,字母t代表"toolkit",在Xt,Xtoolkit等中得到灵感。......