在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Linux/ Apache通用集合Bag接口
Apache Commons Collections教程
Apache通用集合開發(fā)環(huán)境
Apache通用集合過濾對象
Commons Collections簡介
Apache通用集合聯(lián)合
Apache OrderedMap接口
Apache通用集合Bag接口
Apache通用集合安全空檢查
Apache通用集合差集
Apache通用集合包函關系
Apache通用集合BidiMap接口
Apache通用集合相交
Apache通用集合轉(zhuǎn)換對象
Apache通用集合合并與排序
Apache通用集合忽略Null
Apache通用集合MapIterator接口

Apache通用集合Bag接口

新的接口被添加到支持bag。 Bag接口定義了一個集合,它可以計算一個對象出現(xiàn)在集合中的次數(shù)。 例如,如果Bag包含{a,a,b,c},則getCount("a")方法將返回2,而uniqueSet()返回唯一值。

接口聲明

以下是org.apache.commons.collections4.Bag<E>接口的聲明 -

public interface Bag<E>
   extends Collection<E>
編號 方法 描述
1 boolean add(E object) (沖突)將指定的對象到Bag的一個副本。
2 boolean add(E object, int nCopies) 將指定對象的nCopies副本添加到Bag中。
3 boolean containsAll(Collection<?> coll) (沖突)如果包包含給定集合中的所有元素,并且尊重基數(shù),則返回true。
4 int getCount(Object object) 返回包中當前給定對象的出現(xiàn)次數(shù)(基數(shù))。
5 Iterator<E> iterator() 在整個成員集上返回一個迭代器,包括由于基數(shù)而產(chǎn)生的副本。
6 boolean remove(Object object) Bag中移除所有給定的對象。
7 boolean remove(Object object, int nCopies) Bag中刪除指定對象的nCopies副本。
8 boolean removeAll(Collection<?> coll) 刪除給定集合中的所有元素,尊重基數(shù)。
9 boolean retainAll(Collection<?> coll) 移除不在給定集合中的所有Bag成員,尊重基數(shù)。
10 int size() 返回Bag中所有類型對象的總數(shù)。
11 Set<E> uniqueSet() 返回Bag中的一組唯一元素。

方法繼承

該接口從以下接口繼承方法 -

  • java.util.Collection

Bag接口示例

import org.apache.commons.collections4.Bag;
import org.apache.commons.collections4.bag.HashBag;

public class BagTester {
   public static void main(String[] args) {
      Bag<String> bag = new HashBag<>();

      //add "a" two times to the bag.
      bag.add("a" , 2);

      //add "b" one time to the bag.
      bag.add("b");

      //add "c" one time to the bag.
      bag.add("c");

      //add "d" three times to the bag.
      bag.add("d",3);

      //get the count of "d" present in bag.
      System.out.println("d is present " + bag.getCount("d") + " times.");
      System.out.println("bag: " +bag);

      //get the set of unique values from the bag
      System.out.println("Unique Set: " +bag.uniqueSet());

      //remove 2 occurrences of "d" from the bag
      bag.remove("d",2);
      System.out.println("2 occurences of d removed from bag: " +bag);
      System.out.println("d is present " + bag.getCount("d") + " times.");
      System.out.println("bag: " +bag);
      System.out.println("Unique Set: " +bag.uniqueSet());
   }
}

執(zhí)行上面示例代碼,得到以下結果 -

d is present 3 times.
bag: [2:a,1:b,1:c,3:d]
Unique Set: [a, b, c, d]
2 occurences of d removed from bag: [2:a,1:b,1:c,1:d]
d is present 1 times.
bag: [2:a,1:b,1:c,1:d]
Unique Set: [a, b, c, d]