一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 如何巧用HashMap一行代碼統計單詞出現次數詳解

如何巧用HashMap一行代碼統計單詞出現次數詳解

2020-07-20 11:41flydean Java教程

這篇文章主要給大家介紹了關于如何巧用HashMap一行代碼統計單詞出現次數的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧

前言

JDK是在一直在迭代更新的,很多我們熟悉的類也悄悄的添加了一些新的方法特性。比如我們最常用的HashMap。

今天給大家講一下HashMap在JDK8中添加的兩個新方法compute和merge,從而實現一行代碼實現單詞統計的功能。一起來看看吧。

愛在JDK8之前

JDK8為我們引入了很多非常非常有用新特性,比如Stream和lambda表達式,可以讓我們的程序更加簡潔。

如果我們需要統計一個數組中單詞出現的次數該怎么做呢?

這里不是講算法,所以可以直接使用HashMap:

?
1
2
3
4
5
6
7
8
9
10
11
12
public void countBefore8(){
  Map<String,Integer> wordCount= new HashMap<>();
  String[] wordArray= new String[]{"we","are","the","world","we"};
  for(String word: wordArray){
   //如果存在則加1,否則將值設置為1
   if(wordCount.containsKey(word)) {
    wordCount.put(word, wordCount.get(word) + 1);
   }else{
    wordCount.put(word, 1);
   }
  }
 }

基本上流程是上面樣子的。我們對數組進行遍歷,然后判斷這個單詞是否存在于hashMap中,如果存在則+1。

邏輯很簡單,但是看起來有些臃腫。

別怕,我們有JDK8。

JDK8中使用compute

先看下JDK8中compute的定義:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
default V compute(K key,
   BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
  Objects.requireNonNull(remappingFunction);
  V oldValue = get(key);
 
  V newValue = remappingFunction.apply(key, oldValue);
  if (newValue == null) {
   // delete mapping
   if (oldValue != null || containsKey(key)) {
    // something to remove
    remove(key);
    return null;
   } else {
    // nothing to do. Leave things as they were.
    return null;
   }
  } else {
   // add or replace old mapping
   put(key, newValue);
   return newValue;
  }
 }

可以看到compute有第二個參數BiFunction,BiFunction就是一個函數,輸入兩個參數,返回一個參數。

BiFunction的兩個參數分別是key和key所對應的oldValue。

可考慮到我們的單詞統計,我們可以直接將oldValue+1 即可。所以使用compute,可以將方法改寫為:

?
1
2
3
4
5
6
7
8
public void countAfter8WithCompute(){
  Map<String,Integer> wordCount= new HashMap<>();
  String[] wordArray= new String[]{"we","are","the","world","we"};
  Arrays.asList(wordArray).forEach(word ->{
   wordCount.putIfAbsent(word,0);
   wordCount.compute(word,(w,count)->count+1);
  });
 }

當然,我們可以將putIfAbsent放到compute中:

?
1
2
3
4
5
public void countAfter8WithCompute2(){
  Map<String,Integer> wordCount= new HashMap<>();
  String[] wordArray= new String[]{"we","are","the","world","we"};
  Arrays.asList(wordArray).forEach(word -> wordCount.compute(word,(w, count)->count == null ? 1 : count + 1));
 }

一行代碼就完成了。

JDK8中使用merge

再看看merge方法:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
default V merge(K key, V value,
   BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
  Objects.requireNonNull(remappingFunction);
  Objects.requireNonNull(value);
  V oldValue = get(key);
  V newValue = (oldValue == null) ? value :
     remappingFunction.apply(oldValue, value);
  if (newValue == null) {
   remove(key);
  } else {
   put(key, newValue);
  }
  return newValue;
 }

merge方法需要3個參數,第一個參數是key,第二個參數是key對應的oldValue為空的值,也就是為空的默認值,第三個參數是一個BiFunction參數。

不同的是BiFunction的第一個參數是oldValue,第二個參數是value。

生成newValue的邏輯是:如果oldValue不存在,則使用value。如果oldValue存在,則調用BiFunction對oldValue和Value進行合并。

我們可以寫出相應的代碼如下:

?
1
2
3
4
5
public void countAfter8WithMerge(){
  Map<String,Integer> wordCount= new HashMap<>();
  String[] wordArray= new String[]{"we","are","the","world","we"};
  Arrays.asList(wordArray).forEach(word->wordCount.merge(word, 1, (oldCount, one) -> oldCount + one));
 }

后面的函數可以用Integer::sum替代:

?
1
2
3
4
5
public void countAfter8WithMerge(){
  Map<String,Integer> wordCount= new HashMap<>();
  String[] wordArray= new String[]{"we","are","the","world","we"};
  Arrays.asList(wordArray).forEach(word->wordCount.merge(word, 1, Integer::sum));
 }

本文的例子 https://github.com/ddean2009/learn-java-base-9-to-20/tree/master/java-base

原文鏈接:http://www.flydean.com/wordcount-in-one-line/

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日韩在线视频二区 | 精品一区二区三区免费毛片 | 欧美粗黑巨大gay | 成年人免费观看 | 欧美视频免费 | 91精品国产一区 | 亚洲视频男人的天堂 | 暖暖日本在线观看免费 | 毛片资源| 亚洲成年网站在线观看 | 国产极品精频在线观看 | 亚洲v日韩v欧美在线观看 | 国产香蕉一区二区精品视频 | 2022最新a精品视频在线观看 | 日本在线一区 | 91在线视频播放 | 波多野结衣在线中文字幕 | 高h短篇辣肉各种姿势bl | 四虎精品成人免费观看 | 91精品婷婷国产综合久久8 | 欧美专区综合 | 男人猛激烈吃奶gif动态图 | 午夜香蕉成视频人网站高清版 | 日韩在线毛片 | 亚洲国产成人精品不卡青青草原 | 加勒比福利 | 亚洲AV 无码AV 中文字幕 | 午夜在线播放免费人成无 | 国产美女屁股直流白浆视频无遮挡 | 大ji吧快给我别停受不了视频 | 国产裸舞在线一区二区 | 第一福利在线导航 | 男人天堂bt | 我被黑人彻底征服的全文 | 日本最大的黄色网站 | 爽爽窝窝午夜精品一区二区 | 性关系视频网站 | 2021久久| 秋霞理论在一l级毛片 | 糖心vlog麻豆精东影业传媒 | 国内精品久久久久久野外 |