首页 > 其他分享 >HashMap 、LinkedHashMap 、 Hashtable 、ConcurrentHashMap的使用区别和Collections -- 集合工具类

HashMap 、LinkedHashMap 、 Hashtable 、ConcurrentHashMap的使用区别和Collections -- 集合工具类

时间:2023-06-19 20:31:41浏览次数:38  
标签:map ConcurrentHashMap HashMap -- System put key println out

LinkedHashMap

LinkedHashMap的使用

public static void main(String[] args) {

		LinkedHashMap<String,Integer> map = new LinkedHashMap<>();

		//添加元素
		Integer put1 = map.put("大文", 25);
		Integer put2 = map.put("小文", 26);
		Integer put3 = map.put("小王", 29);
		Integer put4 = map.put("小李", 28);
		Integer put5 = map.put("小博", 21);
		Integer put6 = map.put("小黄", 26);
		System.out.println("put1:" + put1);//null
		System.out.println("put2:" + put2);//null
		System.out.println("put3:" + put3);//null
		System.out.println("put4:" + put4);//null
		System.out.println("put5:" + put5);//null
		System.out.println("put6:" + put6);//null


		//替换,返回被替换的值
		Integer put = map.put("小黄", 27);
		System.out.println("put:" + put);//26

		//替换,返回被替换的值
		Integer replace1 = map.replace("小黄", 28);
		System.out.println("replace1:" + replace1);//27

		//替换,返回是否替换成功的boolean值
		boolean replace2 = map.replace("小黄", 28, 29);//key - 被替换值 -替换值
		System.out.println("replace2:" + replace2);//true

		//将newMap1中所有的映射关系添加到map集合中
		LinkedHashMap<String, Integer> newMap1 = new LinkedHashMap<>();
		newMap1.put("aaa", 10);
		newMap1.put("bbb", 20);
		newMap1.put("ccc", 30);
		newMap1.put("ddd", 40);
		newMap1.put("ddd", 50);
		map.putAll(newMap1);

		//如果key存在则返回value,如果key不存在就添加
		Integer putIfAbsent = map.putIfAbsent("小王", 123);
		System.out.println("putIfAbsent:" + putIfAbsent);//29

		//通过key获取对应的value值
		Integer integer = map.get("小李");
		System.out.println("通过key获取对应的value值:" + integer);

		//通过key获取对应的value值,如果没有key就返回默认值666
		Integer orDefault = map.getOrDefault("小王111", 666);
		System.out.println("通过key获取对应的value值:" + orDefault);

		//清空所有的元素
		//map.clear();

		System.out.println("判断map集合中是否包含某个key:" + map.containsKey("小王"));//true
		System.out.println("判断map集合中是否包含某个value:" + map.containsValue(28));//true
		System.out.println("判断map集合是否有元素:" + map.isEmpty());//false
		//有元素就返回false

		//根据key删除映射关系
		map.remove("小博");
		//根据key+value删除映射关系
		map.remove("小李", 28);

		System.out.println("获取集合元素个数:" + map.size());//8

		//获取map集合中所有的value
		Collection<Integer> values = map.values();
		System.out.println(Arrays.toString(values.toArray()));//集合->数组->字符串

		System.out.println("---------------------");

		//遍历 -- keySet()
		//思路:获取map中所有的key,存入Set集合中,遍历Set集合依次把key获取出来,利用map.get(key)获取出对应的value值
		Set<String> keySet = map.keySet();
		for (String key : keySet) {
			Integer value = map.get(key);
			System.out.println(key + "--" + value);//获取出来的也是无序的

		}
		System.out.println("--------------------");

		//遍历 - entrySet()
		//思路:获取map中所有的映射关系对象(Entry),存入Set集合,遍历Set集合依次把Entry取出来,获取Entry中的key和value
		Set<Entry<String,Integer>> entrySet = map.entrySet();
		for (Entry<String, Integer> entry : entrySet) {
			String key = entry.getKey();
			Integer value = entry.getValue();
			System.out.println(key + "--" + value);
		}
		
	}
继承关系:

classs LinkedHashMap extends HashMap

特点:

有序 + key去重

Hashtable

Hashtable的使用

public static void main(String[] args) {

		Hashtable<String,Integer> map = new Hashtable<>();

		//添加元素
		Integer put1 = map.put("大文", 25);
		Integer put2 = map.put("小文", 26);
		Integer put3 = map.put("小王", 29);
		Integer put4 = map.put("小李", 28);
		Integer put5 = map.put("小博", 21);
		Integer put6 = map.put("小黄", 26);
		System.out.println("put1:" + put1);//null
		System.out.println("put2:" + put2);//null
		System.out.println("put3:" + put3);//null
		System.out.println("put4:" + put4);//null
		System.out.println("put5:" + put5);//null
		System.out.println("put6:" + put6);//null


		//替换,返回被替换的值
		Integer put = map.put("小黄", 27);
		System.out.println("put:" + put);//26

		//替换,返回被替换的值
		Integer replace1 = map.replace("小黄", 28);
		System.out.println("replace1:" + replace1);//27

		//替换,返回是否替换成功的boolean值
		boolean replace2 = map.replace("小黄", 28, 29);//key - 被替换值 -替换值
		System.out.println("replace2:" + replace2);//true

		//将newMap1中所有的映射关系添加到map集合中
		Hashtable<String, Integer> newMap1 = new Hashtable<>();
		newMap1.put("aaa", 10);
		newMap1.put("bbb", 20);
		newMap1.put("ccc", 30);
		newMap1.put("ddd", 40);
		newMap1.put("ddd", 50);
		map.putAll(newMap1);

		//如果key存在则返回value,如果key不存在就添加
		Integer putIfAbsent = map.putIfAbsent("小王", 123);
		System.out.println("putIfAbsent:" + putIfAbsent);//29

		//通过key获取对应的value值
		Integer integer = map.get("小李");
		System.out.println("通过key获取对应的value值:" + integer);

		//通过key获取对应的value值,如果没有key就返回默认值666
		Integer orDefault = map.getOrDefault("小王111", 666);
		System.out.println("通过key获取对应的value值:" + orDefault);

		//清空所有的元素
		//map.clear();

		System.out.println("判断map集合中是否包含某个key:" + map.containsKey("小王"));//true
		System.out.println("判断map集合中是否包含某个value:" + map.containsValue(28));//true
		System.out.println("判断map集合是否有元素:" + map.isEmpty());//false
		//有元素就返回false

		//根据key删除映射关系
		map.remove("小博");
		//根据key+value删除映射关系
		map.remove("小李", 28);

		System.out.println("获取集合元素个数:" + map.size());//8

		//获取map集合中所有的value
		Collection<Integer> values = map.values();
		System.out.println(Arrays.toString(values.toArray()));//集合->数组->字符串

		System.out.println("---------------------");

		//遍历 -- keySet()
		//思路:获取map中所有的key,存入Set集合中,遍历Set集合依次把key获取出来,利用map.get(key)获取出对应的value值
		Set<String> keySet = map.keySet();
		for (String key : keySet) {
			Integer value = map.get(key);
			System.out.println(key + "--" + value);//获取出来的也是无序的

		}
		System.out.println("--------------------");

		//遍历 - entrySet()
		//思路:获取map中所有的映射关系对象(Entry),存入Set集合,遍历Set集合依次把Entry取出来,获取Entry中的key和value
		Set<Entry<String,Integer>> entrySet = map.entrySet();
		for (Entry<String, Integer> entry : entrySet) {
			String key = entry.getKey();
			Integer value = entry.getValue();
			System.out.println(key + "--" + value);
		}
		
	}

特点:

无序 + key去重 + 线程安全(底层是方法上加锁了的 + 效率低)

ConcurrentHashMap

ConcurrentHashMap的使用\

public static void main(String[] args) {

		ConcurrentHashMap<String,Integer> map = new ConcurrentHashMap<>();

		//添加元素
		Integer put1 = map.put("大文", 25);
		Integer put2 = map.put("小文", 26);
		Integer put3 = map.put("小王", 29);
		Integer put4 = map.put("小李", 28);
		Integer put5 = map.put("小博", 21);
		Integer put6 = map.put("小黄", 26);
		System.out.println("put1:" + put1);//null
		System.out.println("put2:" + put2);//null
		System.out.println("put3:" + put3);//null
		System.out.println("put4:" + put4);//null
		System.out.println("put5:" + put5);//null
		System.out.println("put6:" + put6);//null


		//替换,返回被替换的值
		Integer put = map.put("小黄", 27);
		System.out.println("put:" + put);//26

		//替换,返回被替换的值
		Integer replace1 = map.replace("小黄", 28);
		System.out.println("replace1:" + replace1);//27

		//替换,返回是否替换成功的boolean值
		boolean replace2 = map.replace("小黄", 28, 29);//key - 被替换值 -替换值
		System.out.println("replace2:" + replace2);//true

		//将newMap1中所有的映射关系添加到map集合中
		ConcurrentHashMap<String, Integer> newMap1 = new ConcurrentHashMap<>();
		newMap1.put("aaa", 10);
		newMap1.put("bbb", 20);
		newMap1.put("ccc", 30);
		newMap1.put("ddd", 40);
		newMap1.put("ddd", 50);
		map.putAll(newMap1);

		//如果key存在则返回value,如果key不存在就添加
		Integer putIfAbsent = map.putIfAbsent("小王", 123);
		System.out.println("putIfAbsent:" + putIfAbsent);//29

		//通过key获取对应的value值
		Integer integer = map.get("小李");
		System.out.println("通过key获取对应的value值:" + integer);

		//通过key获取对应的value值,如果没有key就返回默认值666
		Integer orDefault = map.getOrDefault("小王111", 666);
		System.out.println("通过key获取对应的value值:" + orDefault);

		//清空所有的元素
		//map.clear();

		System.out.println("判断map集合中是否包含某个key:" + map.containsKey("小王"));//true
		System.out.println("判断map集合中是否包含某个value:" + map.containsValue(28));//true
		System.out.println("判断map集合是否有元素:" + map.isEmpty());//false
		//有元素就返回false

		//根据key删除映射关系
		map.remove("小博");
		//根据key+value删除映射关系
		map.remove("小李", 28);

		System.out.println("获取集合元素个数:" + map.size());//8

		//获取map集合中所有的value
		Collection<Integer> values = map.values();
		System.out.println(Arrays.toString(values.toArray()));//集合->数组->字符串

		System.out.println("---------------------");

		//遍历 -- keySet()
		//思路:获取map中所有的key,存入Set集合中,遍历Set集合依次把key获取出来,利用map.get(key)获取出对应的value值
		Set<String> keySet = map.keySet();
		for (String key : keySet) {
			Integer value = map.get(key);
			System.out.println(key + "--" + value);//获取出来的也是无序的

		}
		System.out.println("--------------------");

		//遍历 - entrySet()
		//思路:获取map中所有的映射关系对象(Entry),存入Set集合,遍历Set集合依次把Entry取出来,获取Entry中的key和value
		Set<Entry<String,Integer>> entrySet = map.entrySet();
		for (Entry<String, Integer> entry : entrySet) {
			String key = entry.getKey();
			Integer value = entry.getValue();
			System.out.println(key + "--" + value);
		}
		
	}

经验:

多线程下直接使用ConcurrentHashMap

特点:

无序 + key去重 + 线程安全(底层局部加锁 + CAS,效率更高)

Collections -- 集合工具类

public static void main(String[] args) {

		ArrayList<Integer> list = new ArrayList<>();

		//批量添加数据
		Collections.addAll(list, 5,2,7,9,1,6,3,8,4);

		//排序 -- 外置比较
		list.sort(new Comparator<Integer>() {
			@Override
			public int compare(Integer o1, Integer o2) {
				return Integer.compare(o1, o2);
			}
		});

		//查询
		int binarySearch = Collections.binarySearch(list, 2);
		System.out.println("查询元素在数组中的下标:" + binarySearch);

		Integer max = Collections.max(list);
		System.out.println("最大值为:" + max);

		Integer min = Collections.min(list);
		System.out.println("最小值为:" + min);

		//替换
		Collections.fill(list, 888);//全部替换

		//遍历
		System.out.println(Arrays.toString(list.toArray()));

	}

HashMap 、LinkedHashMap 、 Hashtable 、ConcurrentHashMap的区别


特点的区别

HashMap

无序

key去重

线程不安全


LinkedHashMap

有序

key去重

线程不安全


Hashtable

无序

key去重

线程安全

方法上加锁,效率低,在多线程上已弃用

ConcurrentHashMap

无序

key去重

线程安全

局部加锁+CAS,效率高 ,在多线程上直接用

能否存储空键、空值的区别:

*/

能否存储空键、空值的区别:



HashMap

ok


LinkedHashMap

ok


Hashtable

no

会报空指针异常

ConcurrentHashMap

no

会报空指针异常,线程安全的不会允许有空指针存在

标签:map,ConcurrentHashMap,HashMap,--,System,put,key,println,out
From: https://blog.51cto.com/u_16154651/6517268

相关文章

  • MYSQL经典练习题
    题目来源:https://blog.csdn.net/flycat296/article/details/63681089Github地址:https://github.com/bladeXue/sql50添加测试数据库信息#创建数据库createdatabasesql50;usesql50;#学生表createtableStudent(SIdvarchar(10),Snamevarchar(10),Sagedatetime,Sse......
  • To ChatGPT:让你更加随意地使用所有ChatGPT应用
    现在其实已经有很多在线的llm服务了,当然也存在许多开源部署方案,但是不知道大家有没有发现一个问题,目前基于ChatGPT开发的应用,都是使用的OpenAI的接口。换句话说,如果没有OpenAI账号,就没有办法使用这些应用。但是其实这些应用并不是强依赖于OpenAI的接口,其他的在线llm服务也是可以的......
  • PaddleOCR环境配置踩坑记录
    前言PaddleOCR的效果目前是开源下最好用的,但是配置环境坑很多,虽然已经配好能用,但是再次使用还是会偶尔报bug,故在此记录一些对应的bug便于查阅。bug对应解决方法1、ImportError:libcudart.so.10.2:cannotopensharedobjectfile:Nosuchfileordirectory找到对应conda环......
  • [Ynoi2019 模拟赛] Yuno loves sqrt technology I
    题目Link分块,首先预处理所有整块之间的答案,这部分用类似莫队二离的手法可以改成\(O(n)\)次插入和\(O(n\sqrt{n})\)查询,然后根号平衡一手做到\(O(n\sqrt{n})\);空间自然也是能线性的。当然更直白的说法是,直接预处理\(f(i,j)\)表示前\(i\)块中\(>j\)的元素个数。然后考......
  • Manacher算法学习笔记
    Manacher算法是什么Manacher算法就是马拉车。Manacher算法就是用于解决回文子串的个数的。问题引入P3805:【模板】manacher算法题目大意给出一个只由小写英文字符\(\texttta,\textttb,\textttc,\ldots\texttty,\textttz\)组成的字符串\(S\),求\(S\)中最长回文串......
  • 2.键入网址到网页显示的过程
    当我们输入网址到最后显示请求页面的简要流程图如下: 1.1HTTP当我们向浏览器输入网址后,浏览器首先就是解析URL,从而生成发送给Web服务器的请求信息。 URL组成元素:URL开头表示访问数据时使用的协议,//后面的字符串表示服务器的名称,后面的蓝色部分表示服务器所在目录及......
  • mix-blend-mode和background-blend-mode应用场景
    mix-blend-mode使多重叠元素的颜色发生混合,包括元素与元素,元素与图片background-blend-mode使得多个背景发生混合,包括背景图与背景图,背景图与背景色isolation:isolate可以创建层叠上下文,就可以阻断mix-blend-mode,使多个元素能分组进行混合,不干扰常用场景1.图片后方的元素......
  • 自定义 v-model 指令
    //two.jsexportdefault{bind(el,binding,vnode){console.log(binding);el.value=binding.valueif(/\.async/.test(binding.rawName)){el.onchange=handleFn;}else{el.oninput=handleFn;......
  • python二维列表(矩阵转置)
    1.方法一lst1=[[2,0,0,2],[2,1,2,1],[3,1,1,2],[0,1,0,1],]lst1[:]=[list(reversed(item))foriteminlst1]print(lst1)2.方法二lst2=[[2,0,0,2],[2,1,2,1],[3,1,1,2],[0,1,0,1],]lst2[:]=[list(item)foriteminzip(*l......
  • Java 中 HashMap 初始化时赋值 匿名类
    Java中HashMap初始化时赋值匿名类https://www.shuzhiduo.com/A/kjdwWMPOdN/1、HashMap初始化的文艺写法HashMap是一种常用的数据结构,一般用来做数据字典或者Hash查找的容器。普通青年一般会这么初始化:HashMap<String,String>map=newHashMap<String,String>();map......