色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

Java并發(fā)編程之詳解ConcurrentHashMap類

瀏覽:6日期:2022-08-10 08:37:16
前言

由于Java程序員常用的HashMap的操作方法不是同步的,所以在多線程環(huán)境下會導致存取操作數(shù)據(jù)不一致的問題,Map接口的另一個實現(xiàn)類Hashtable 雖然是線程安全的,但是在多線程下執(zhí)行效率很低。為了解決這個問題,在java 1.5版本中引入了線程安全的集合類ConcurrentMap。

Java并發(fā)編程之詳解ConcurrentHashMap類

java.util.concurrent.ConcurrentMap接口是Java集合類框架提供的線程安全的map,這意味著多線程同時訪問它,不會影響map中每一條數(shù)據(jù)的一致性。ConcurrentMap接口有兩個實現(xiàn)類ConcurrentHashMap和ConcurrentSkipListMap,經(jīng)常被使用的是ConcurrentHashMap,我們來重點關注它。

一、創(chuàng)建ConcurrentHashMap對象

通過下面的代碼創(chuàng)建ConcurrentHashMap

// 創(chuàng)建容量為8,負載系數(shù)為0.6的ConcurrentHashMapConcurrentHashMap<Key, Value> numbers = new ConcurrentHashMap<>(8, 0.6f);

使用上面的代碼,我們創(chuàng)建一個叫做numbers的ConcurrentHashMap對象。

Key - 用于關聯(lián)Map中每個元素的唯一標識 Value - Map中每個元素,可以通過key值獲取value

需要我們特別注意的是new ConcurrentHashMap<>(8, 0.6).

capacity容量 - 第一個參數(shù)表示這個map的容量是8,也就是說這個對象可以存儲8個鍵值對 loadFactor負載因子 - 這個map對象的負載因子是 0.6. 這意味著,每當我們的哈希表被填滿60%的時候,條目就會被移動到一個新的哈希表,其容量大小是原來哈希表的兩倍。

默認容量與負載因子我們還可以通過下面的代碼初始化一個ConcurrentHashMap對象,默認情況下capacity=16,loadFactor=0.75

ConcurrentHashMap<Key, Value> numbers1 = new ConcurrentHashMap<>();二、ConcurrentHashMap常用方法2.1. 向ConcurrentHashMap插入元素 put(K,V) - 向map中插入key/value 鍵值對數(shù)據(jù) putAll(map) - 把另一個map中的所有entries插入到當前的map中 putIfAbsent(K,V) - 向map中插入key/value 鍵值對數(shù)據(jù),如果該鍵值對的key在map不存在則插入數(shù)據(jù),否則不做操作。

import java.util.concurrent.ConcurrentHashMap;class Main { public static void main(String[] args) {// 創(chuàng)建ConcurrentHashMap 用于保存偶數(shù)ConcurrentHashMap<String, Integer> evenNumbers = new ConcurrentHashMap<>();// 使用put()方法插入數(shù)據(jù)evenNumbers.put('Two', 2);evenNumbers.put('Four', 4);// 使用putIfAbsent()插入數(shù)據(jù)evenNumbers.putIfAbsent('Six', 6);System.out.println('偶數(shù)集合ConcurrentHashMap: ' + evenNumbers);//創(chuàng)建ConcurrentHashMap用于保存整數(shù)ConcurrentHashMap<String, Integer> numbers = new ConcurrentHashMap<>();numbers.put('One', 1);// 使用putAll()插入數(shù)據(jù)numbers.putAll(evenNumbers);System.out.println('整數(shù)集合ConcurrentHashMap: ' + numbers); }}

輸出結果:

偶數(shù)集合ConcurrentHashMap: {Six=6, Four=4, Two=2}整數(shù)集合ConcurrentHashMap: {Six=6, One=1, Four=-4, Two=2}2.2.批量獲取ConcurrentHashMap 元素 entrySet()- 獲取 map中key/value 鍵值對集合 keySet()- 獲取map中所有的key的集合 values()- 獲取map中所有的value的集合

import java.util.concurrent.ConcurrentHashMap;class Main { public static void main(String[] args) {ConcurrentHashMap<String, Integer> numbers = new ConcurrentHashMap<>();numbers.put('One', 1);numbers.put('Two', 2);numbers.put('Three', 3);System.out.println('ConcurrentHashMap: ' + numbers);// 獲取 map中key/value 鍵值對集合System.out.println('Key/Value mappings: ' + numbers.entrySet());// 獲取map中所有的key的集合System.out.println('Keys: ' + numbers.keySet());// 獲取map中所有的value的集合System.out.println('Values: ' + numbers.values()); }}

輸出結果

ConcurrentHashMap: {One=1, Two=2, Three=3}Key/Value mappings: [One=1, Two=2, Three=3]Keys: [One, Two, Three]Values: [1, 2, 3]

2.3. 獲取指定Key元素的value值 get() - 獲取指定key元素的value值,如果key不存在返回null getOrDefault() - 獲取指定key元素的value值,如果key不存在返回一個指定的默認值

import java.util.concurrent.ConcurrentHashMap;class Main { public static void main(String[] args) {ConcurrentHashMap<String, Integer> numbers = new ConcurrentHashMap<>();numbers.put('One', 1);numbers.put('Two', 2);numbers.put('Three', 3);System.out.println('ConcurrentHashMap: ' + numbers);// 獲取指定key元素的value值,如果key不存在返回nullint value1 = numbers.get('Three');System.out.println('Using get(): ' + value1);// 獲取指定key元素的value值,如果key不存在返回一個指定的默認值int value2 = numbers.getOrDefault('Five', 5);System.out.println('Using getOrDefault(): ' + value2); }}

輸出結果

ConcurrentHashMap: {One=1, Two=2, Three=3}Using get(): 3Using getOrDefault(): 5

2.4.移除ConcurrentHashMap中的元素 remove(key) - 根據(jù)指定的key刪除map中的元素,并將該元素返回 remove(key, value) - 只有當map中存在指定的鍵映射到指定的值時,才會從map中刪除條目,并返回一個布爾值。返回true表示刪除成功,否則表示map中沒有這個鍵值對。

import java.util.concurrent.ConcurrentHashMap;class Main { public static void main(String[] args) {ConcurrentHashMap<String, Integer> numbers = new ConcurrentHashMap<>();numbers.put('One', 1);numbers.put('Two', 2);numbers.put('Three', 3);System.out.println('ConcurrentHashMap: ' + numbers);// 根據(jù)指定的key刪除map中的元素,并將該元素返回int value = numbers.remove('Two');System.out.println('Removed value: ' + value);// 只有當map中存在指定的鍵映射到指定的值時,才會從map中刪除條目,并返回一個布爾值。boolean result = numbers.remove('Three', 3);System.out.println('Is the entry {Three=3} removed? ' + result);System.out.println('Updated ConcurrentHashMap: ' + numbers); }}

輸出結果

ConcurrentHashMap: {One=1, Two=2, Three=3}Removed value: 2Is the entry {Three=3} removed? TrueUpdated ConcurrentHashMap: {One=1}

到此這篇關于Java并發(fā)編程之詳解ConcurrentHashMap類的文章就介紹到這了,更多相關Java ConcurrentHashMap內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持好吧啦網(wǎng)!

標簽: Java
相關文章:
主站蜘蛛池模板: 精品日本久久久久久久久久 | 国产成人毛片精品不卡在线 | 国产一区私人高清影院 | 亚洲国产成人影院播放 | 国产成人一区二区三区在线播放 | 成人自拍网| 日本三级香港三级人妇99视 | 国产精品三级a三级三级午夜 | 国产二区三区 | 一级做性色a爱片久久片 | 国产成人亚洲精品影院 | 一级特黄特黄的大片免费 | 欧美巨大另类极品videohd | 91免费网站在线看入口黄 | 欧美日韩色黄大片在线视频 | 成人毛片免费视频播放 | 午夜看毛片 | 久青草国产手机在线观 | 国产毛片一级 | a级一片 | 亚洲美女网址 | 久久亚洲国产成人亚 | 韩国黄色一级毛片 | 久久这里一区二区精品 | 国产自在自线午夜精品视频 | 亚洲综合爱久久影院 | 欧美最大成人毛片视频网站 | 欧美另类69xxxxx 视频 | 国产精品爱久久久久久久小 | 久久爱wwwww 久久爱www成人 | 521a久久九九久久精品 | 亚洲综合精品成人 | 午夜爽爽爽男女免费观看hd | 香港三级日本三级三级人妇 | 免费国产一区二区三区 | 国产精品一级香蕉一区 | 日韩精品中文字幕一区三区 | 精品午夜寂寞影院在线观看 | 日本红怡院亚洲红怡院最新 | 亚洲精品国产福利一区二区三区 | 久久精品vr中文字幕 |