自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

你只會(huì)用 map.put?試試 Java 8 compute ,操作 Map 更輕松!

開發(fā) 后端
今天棧長(zhǎng)分享一個(gè)實(shí)用的 Java 8 開發(fā)技能,那就是 Map 接口中增加的 compute 方法,給 Map 集合計(jì)算更新用的。

 [[424111]]

今天棧長(zhǎng)分享一個(gè)實(shí)用的 Java 8 開發(fā)技能,那就是 Map 接口中增加的 compute 方法,給 Map 集合計(jì)算更新用的。

compute簡(jiǎn)介

如下所示,Java 8 在 Map 和 ConcurrentMap 接口中都增加了 3 個(gè) compute 方法,說明也是支持多線程并發(fā)安全操作的。

這三個(gè)方法的區(qū)別:

  •  compute:計(jì)算并更新值
  •  computeIfAbsent:Value不存在時(shí)才計(jì)算
  •  computeIfPresent:Value存在時(shí)才計(jì)算

compute有啥用?

話說這有什么卵用?

先看看沒用 Java 8 的一個(gè)小示例: 

  1. /**  
  2. * 公眾號(hào):Java技術(shù)棧  
  3. */  
  4. private static void preJava8() {  
  5.     List<String> animals = Arrays.asList("dog", "cat", "cat", "dog", "fish", "dog");  
  6.     Map<String, Integer> map = new HashMap<>();  
  7.     for(String animal : animals){  
  8.         Integer count = map.get(animal);  
  9.         map.put(animal, count == null ? 1 : ++count);  
  10.     }  
  11.     System.out.println(map);  

輸出:

{cat=2, fish=1, dog=3}

這是一個(gè)統(tǒng)計(jì)一個(gè)列表中每個(gè)動(dòng)物的數(shù)量,代碼再怎么精簡(jiǎn)都需要一步 get 操作,判斷集合中是否有元素再確定是初始化:1,還是需要 +1。

很多時(shí)候,這個(gè) get 操作顯然是毫無(wú)必要的,所以 Java 8 提供了 3 個(gè) compute 方法,來(lái)看看怎么用吧!

Java 8 compute 實(shí)現(xiàn)方式: 

  1. /**  
  2. * 公眾號(hào):Java技術(shù)棧  
  3. */  
  4. private static void inJava8() {  
  5.     List<String> animals = Arrays.asList("dog", "cat", "cat", "dog", "fish", "dog");  
  6.     Map<String, Integer> map = new HashMap<>();  
  7.     for(String animal : animals){  
  8.         map.compute(animal, (k, v) -> v == null ? 1 : ++v);  
  9.     }  
  10.     System.out.println(map);  

使用 compute 方法一行搞定,省去了需要使用 get 取值再判斷的冗余操作,直接就可以獲取元素值并計(jì)算更新,是不是很方便呢?

compute源碼分析

這還是一個(gè)默認(rèn)方法,為什么是默認(rèn)方法,也是為了不改動(dòng)其所有實(shí)現(xiàn)類,關(guān)于默認(rèn)方法的定義可以關(guān)注公眾號(hào)Java技術(shù)棧獲取 Java 8+ 系列教程。 

  1. /**  
  2. * 公眾號(hào):Java技術(shù)棧  
  3. */  
  4. default V compute(K key,  
  5.         BiFunction<? super K, ? super V, ? extends V> remappingFunction) {       
  6.      // 函數(shù)式接口不能為空    
  7.     Objects.requireNonNull(remappingFunction);   
  8.      // 獲取舊值  
  9.     V oldValue = get(key); 
  10.      // 獲取計(jì)算的新值  
  11.     V newValue = remappingFunction.apply(key, oldValue);    
  12.      if (newValue == null) { // 新值為空  
  13.         // delete mapping  
  14.         if (oldValue != null || containsKey(key)) { // 舊值存在時(shí)  
  15.             // 移除該鍵值  
  16.             remove(key);  
  17.             return null;  
  18.         } else {  
  19.             // nothing to do. Leave things as they were.  
  20.             return null;  
  21.         }  
  22.     } else { // 新值不為空  
  23.         // 添加或者覆蓋舊值  
  24.         put(key, newValue);  
  25.         return newValue;  
  26.     }  

實(shí)現(xiàn)邏輯其實(shí)也很簡(jiǎn)單,其實(shí)就是結(jié)合了 Java 8 的函數(shù)式編程讓代碼變得更簡(jiǎn)單了,Java 也越來(lái)越聰明了。

另外兩個(gè)方法我就不演示了,在特定的場(chǎng)合肯定也肯定特別有用,大家知道就好,需要的時(shí)候要知道拿來(lái)用。

本節(jié)教程所有實(shí)戰(zhàn)源碼已上傳到這個(gè)倉(cāng)庫(kù):

https://github.com/javastacks/javastack

本次的分享就到這里了,希望對(duì)大家有用。覺得不錯(cuò),在看、轉(zhuǎn)發(fā)分享一下哦~ 

 

責(zé)任編輯:龐桂玉 來(lái)源: Java編程
相關(guān)推薦

2024-11-08 13:24:43

2021-11-30 10:38:09

splitStringTokenJava

2022-04-28 21:53:52

TypeScriptany類型

2020-04-27 20:55:42

JavaJava 8編程語(yǔ)言

2024-07-10 11:40:15

2010-12-23 15:55:00

上網(wǎng)行為管理

2022-11-07 17:50:36

2020-06-28 16:07:03

HomebrewMacLinux

2024-01-11 11:51:51

Rustmap數(shù)據(jù)結(jié)構(gòu)

2020-03-06 10:25:10

注解Java代碼

2023-12-01 15:46:01

Kubernetes容器

2019-06-03 10:50:14

人工智能Java編程

2025-01-03 08:09:15

2009-12-23 15:41:26

2009-05-20 14:43:38

ibmdwEasyMock測(cè)試

2011-01-19 10:42:15

2022-11-07 16:25:07

JavaScript技巧

2023-06-02 15:53:38

工具Python開發(fā)

2020-02-04 14:07:47

Java技術(shù)開發(fā)

2015-06-01 10:48:00

虛擬機(jī)云計(jì)算云就緒
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)