
// BagTester.java
// https://www.tutorialspoint.com/commons_collections/commons_collections_bag.htm
// https://www.baeldung.com/apache-commons-bag
// Andrew Davison, ad@coe.psu.ac.th, May 2023

/* A bag stores multiple items along with their repetition count

  Bags can be replaced by the MultiSet class -- see MultiSetTester.java

  Usage:
    > compile BagTester.java, SetUtilsTester.java
    > run BagTester
*/


import org.apache.commons.collections4.*;
import org.apache.commons.collections4.bag.*;


public class BagTester
{
  public static void main(String[] args)
  {
    Bag<String> bag = new HashBag<>();
    bag.add("a", 2);   // add "a" two times
    bag.add("b");
    bag.add("c");
    bag.add("d",3);

    System.out.println("bag: " + bag);
    System.out.println("d is present " + bag.getCount("d") + " times");
    System.out.println("Unique set for this bag: " + bag.uniqueSet());

    System.out.println("\nRemoving two d's:");
    bag.remove("d", 2);
    System.out.println("bag:" + bag);
    System.out.println("d is present " + bag.getCount("d") + " times");
    System.out.println("Unique set for this bag: " + bag.uniqueSet());
  } /* main */

}  // end of BagTester class
