日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

巧用HashMap一行代码统计单词出现次数

發布時間:2024/2/28 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 巧用HashMap一行代码统计单词出现次数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 簡介
  • 愛在JDK8之前
  • JDK8中使用compute
  • JDK8中使用merge

簡介

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

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

愛在JDK8之前

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

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

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

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

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

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

別怕,我們有JDK8。

JDK8中使用compute

先看下JDK8中compute的定義:

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 mappingif (oldValue != null || containsKey(key)) {// something to removeremove(key);return null;} else {// nothing to do. Leave things as they were.return null;}} else {// add or replace old mappingput(key, newValue);return newValue;}}

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

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

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

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中:

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方法:

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進行合并。

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

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替代:

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/

最通俗的解讀,最深刻的干貨,最簡潔的教程,眾多你不知道的小技巧等你來發現!

歡迎關注我的公眾號:「程序那些事」,懂技術,更懂你!

超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生

總結

以上是生活随笔為你收集整理的巧用HashMap一行代码统计单词出现次数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。