在之前的文章中曾经为大家介绍了java并发编程的工具:BlockingQueue接口、ArrayBlockingQueue、DelayQueue、LinkedBlockingQueue、PriorityBlockingQueue、SynchronousQueue、BlockingDeque接口,本文为系列文章第八篇。

因为Java程序员罕用的HashMap的操作方法不是同步的,所以在多线程环境下会导致存取操作数据不统一的问题,Map接口的另一个实现类Hashtable 尽管是线程平安的,然而在多线程下执行效率很低。为了解决这个问题,在java 1.5版本中引入了线程平安的汇合类ConcurrentMap

java.util.concurrent.ConcurrentMap接口是Java汇合类框架提供的线程平安的map,这意味着多线程同时拜访它,不会影响map中每一条数据的一致性。ConcurrentMap接口有两个实现类ConcurrentHashMap和ConcurrentSkipListMap,常常被应用的是ConcurrentHashMap,咱们来重点关注它。

1.创立ConcurrentHashMap对象

通过上面的代码创立ConcurrentHashMap

// 创立容量为8,负载系数为0.6的ConcurrentHashMapConcurrentHashMap<Key, Value> numbers = new ConcurrentHashMap<>(8, 0.6f);

应用下面的代码,咱们创立一个叫做numbers的ConcurrentHashMap对象。

  • Key - 用于关联Map中每个元素的惟一标识
  • Value - Map中每个元素,能够通过key值获取value

须要咱们特地留神的是new ConcurrentHashMap<>(8, 0.6).

  • capacity容量 - 第一个参数示意这个map的容量是8,也就是说这个对象能够存储8个键值对.
  • loadFactor负载因子 - 这个map对象的负载因子是 0.6. 这意味着,每当咱们的哈希表被填满60%的时候,条目就会被挪动到一个新的哈希表,其容量大小是原来哈希表的两倍。

默认容量与负载因子
咱们还能够通过上面的代码初始化一个ConcurrentHashMap对象,默认状况下capacity=16,loadFactor=0.75

ConcurrentHashMap<Key, Value> numbers1 = new ConcurrentHashMap<>();

2.ConcurrentHashMap罕用办法

2.1. 向ConcurrentHashMap插入元素

  • put(K,V) - 向map中插入key/value 键值对数据
  • putAll(map) - 把另一个map中的所有entries插入到以后的map中
  • putIfAbsent(K,V) - 向map中插入key/value 键值对数据,如果该键值对的key在map不存在则插入数据,否则不做操作。
import java.util.concurrent.ConcurrentHashMap;class Main {    public static void main(String[] args) {        // 创立ConcurrentHashMap 用于保留偶数        ConcurrentHashMap<String, Integer> evenNumbers = new ConcurrentHashMap<>();        // 应用put()办法插入数据        evenNumbers.put("Two", 2);        evenNumbers.put("Four", 4);        // 应用putIfAbsent()插入数据        evenNumbers.putIfAbsent("Six", 6);        System.out.println("偶数汇合ConcurrentHashMap: " + evenNumbers);        //创立ConcurrentHashMap用于保留整数        ConcurrentHashMap<String, Integer> numbers = new ConcurrentHashMap<>();        numbers.put("One", 1);        // 应用putAll()插入数据        numbers.putAll(evenNumbers);        System.out.println("整数汇合ConcurrentHashMap: " + numbers);    }}

输入后果:

偶数汇合ConcurrentHashMap: {Six=6, Four=4, Two=2}整数汇合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不存在返回null        int 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) - 依据指定的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);        // 依据指定的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}

欢送关注我的博客,外面有很多精品合集

本文转载注明出处(必须带连贯,不能只转文字):字母哥博客 - zimug.com

感觉对您有帮忙的话,帮我点赞、分享!您的反对是我不竭的创作能源! 。另外,笔者最近一段时间输入了如下的精品内容,期待您的关注。

  • 《手摸手教你学Spring Boot2.0》
  • 《Spring Security-JWT-OAuth2一本通》
  • 《实战前后端拆散RBAC权限管理系统》
  • 《实战SpringCloud微服务从青铜到王者》
  • 《VUE深入浅出系列》