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
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 −
- java.util.collectio.
example of bag interface
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]
