Commons Collections 简明教程

Apache Commons Collections - Bag Interface

添加了新接口来支持包。包定义了一个集合,它会计算对象在集合中出现的次数。例如,如果一个包包含 {a, a, b, c},那么 getCount("a") 将返回 2,而 uniqueSet() 将返回唯一值。

New Interfaces are added to supports bags. A Bag defines a collection which, counts the number of times an object appears in the collection. For example, if a Bag contains {a, a, b, c} then getCount("a") will return 2 while uniqueSet() returns the unique values.

Interface Declaration

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

Following is the declaration for org.apache.commons.collections4.Bag<E> interface −

public interface Bag<E>
   extends Collection<E>

Methods

包推理的方法如下:

The methods for bag inference are as follows −

Sr.No.

Method & Description

1

boolean add(E object) (Violation) Adds one copy of the specified object to the Bag.

2

boolean add(E object, int nCopies) Adds nCopies copies of the specified object to the Bag.

3

boolean containsAll(Collection<?> coll) (Violation) Returns true if the bag contains all elements in the given collection, respecting cardinality.

4

int getCount(Object object) Returns the number of occurrences (cardinality) of the given object currently in the bag.

5

Iterator<E> iterator() Returns an Iterator over the entire set of members, including copies due to cardinality.

6

boolean remove(Object object) (Violation) Removes all occurrences of the given object from the bag.

7

boolean remove(Object object, int nCopies) Removes nCopies copies of the specified object from the Bag.

8

boolean removeAll(Collection<?> coll) (Violation) Remove all elements represented in the given collection, respecting cardinality.

9

boolean retainAll(Collection<?> coll) (Violation) Remove any members of the bag that are not in the given collection, respecting cardinality.

10

int size() Returns the total number of items in the bag across all types.

11

Set<E> uniqueSet() Returns a Set of unique elements in the Bag.

Methods Inherited

此接口继承了以下接口中的方法:

This interface inherits methods from the following interface −

  1. java.util.Collectio.

Example of Bag Interface

BagTester.java 的一个示例如下:

An example of BagTester.java is as follows −

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());
   }
}

Output

您将看到以下输出 −

You will see the following output −

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]