新的接口被添加到支持bag
。 Bag
接口定义了一个集合,它可以计算一个对象出现在集合中的次数。
public class BagTester { public static void main(String[] args) { //org.apache.commons.collections4.Bag<E>接口继承自java.util.Collection Bag<String> bag = new HashBag<String>(); bag.add("a",2); bag.add("a"); bag.add("a"); bag.add("a"); bag.add("b"); bag.add("d",3); System.out.println(bag); System.out.println("a出现了:"+bag.getCount("a")+"次"); System.out.println("b出现了:"+bag.getCount("b")+"次"); System.out.println("c出现了:"+bag.getCount("c")+"次"); System.out.println("b出现了:"+bag.getCount("d")+"次"); System.out.println("Set<E> uniqueSet():"+bag.uniqueSet()); System.out.println("删除d:"+bag.remove("d")); System.out.println("删除后:"+bag); System.out.println("删除1个a:"+bag.remove("a",1)); System.out.println("删除后"+bag); System.out.println(bag.containsAll(Arrays.asList("a","b"))); System.out.println(bag.containsAll(Arrays.asList("a","b","c"))); System.out.println(bag.containsAll(Arrays.asList("4","1"))); } }
结果:
[5:a,1:b,3:d] a出现了:5次 b出现了:1次 c出现了:0次 b出现了:3次 Set<E> uniqueSet():[a, b, d] 删除d:true 删除后:[5:a,1:b] 删除1个a:true 删除后[4:a,1:b] true false false org.apache.commons.collections4.bag.AbstractMapBag$BagIterator@66d3c617
结束
org.apache.commons.collections4.Bag<E>
原文:https://www.cnblogs.com/mlyun/p/10839275.html