Java集合-treebag

import org.apache.commons.collections4.Bag;
import org.apache.commons.collections4.bag.TreeBag; import java.util.Arrays; public class ApacheTreeBag { public static void main(String[] args) {
// 將字母轉為生字計算
String INPUT_TEXT = "Hello World! Hello All! Hi World!";
// 創造一個迭代器的集合bag
Bag bag = new TreeBag(Arrays.asList(INPUT_TEXT.split(" "))); // 輸出字數
System.out.println(bag); // print [1:All!,2:Hello,1:Hi,2:World!]- in natural (alphabet) order
// Print all unique words
System.out.println(bag.uniqueSet()); // print [All!, Hello, Hi, World!]- in natural (alphabet) order // 輸出字數出現的頻率
System.out.println("Hello = " + bag.getCount("Hello")); // print 2
System.out.println("World = " + bag.getCount("World!")); // print 2
System.out.println("All = " + bag.getCount("All!")); // print 1
System.out.println("Hi = " + bag.getCount("Hi")); // print 1
System.out.println("Empty = " + bag.getCount("Empty")); // print 0 // Print count all words
System.out.println(bag.size()); //print 6 // Print count unique words
System.out.println(bag.uniqueSet().size()); //print 4
}
}
上一篇:查看linux系统硬盘目录占用大小


下一篇:广域网佰腾玩O2O笑话——它看起来很漂亮,注定不低于