com.google.common.collect.Multiset .count可以對集合中的重複的值彙總統計出現次數,增強了可讀性,極大方便了日常工作中數據的統計。
以下是代碼示例:
import java.util.Set;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.Multiset;
String str ="this is a cat and that is a mice where is the food";
//分割字符串
String[] strArray = str.split(" ");
//存儲到Multiset中
Multiset<String> set = HashMultiset.create();
for(String strTemp:strArray){
set.add(strTemp);
}
//獲取所有的單詞 Set
Set<String> letters = set.elementSet();
for(String temp:letters){
System.out.print(temp+":"+set.count(temp)+"\t");
}
//mice:1 that:1 cat:1 is:3 food:1 a:2 the:1 where:1 this:1 and:1