tor
it = bag.iterator(); while(it.hasNext()) { System.out.print(it.next()+" "); } System.out.println(); } //有序的包 public static void treeBag() { System.out.println("=====有序的包========"); Bag
bag = new TreeBag<>(); bag.add("a"); bag.add("a",5); bag.remove("a",2); bag.add("b"); bag.add("c"); Iterator
it = bag.iterator(); while(it.hasNext()) { System.out.print(it.next()+" "); } System.out.println(); } public static void wordcount(){ String str = "this is a cat and that is a micewhere is the food"; String[] strArray = str.split(" "); Bag
bag = new TreeBag<>(); for(String temp:strArray) { bag.add(temp); } System.out.println("=====统计次数========"); Set
keys = bag.uniqueSet(); for(String letter:keys) { System.out.println(letter+"-->"+bag.getCount(letter)); } } }
运行结果:
=====无序的包========
b c a a a a
=====有序的包========
a a a a b c
=====统计次数========
a-->2
and-->1
cat-->1
food-->1
is-->3
micewhere-->1
that-->1
the-->1
this-->1