A Bag/ultiset stores each object in the collection together with a count of occurrences. Extra methods on the interface allow multiple copies of an object to be added or removed at once. JDK analog is HashMap<T, Integer>, when values is count of copies this key.

| Type | Guava | Apache Commons Collections | GS Collections | JDK | | ———–– | ———––| ———––| ———––| ———––| | Order not defined | HashMultiset | HashBag | HashBag | HashMap | | Sorted | TreeMultiset | TreeBag | TreeBag | TreeMap | | Insertion-order | LinkedHashMultiset | - | - | LinkedHashMap | | Concurrent variant | ConcurrentHashMultiset | SynchronizedBag | SynchronizedBag | Collections.synchronizedMap(HashMap<String, Integer>) | | Concurrent and sorted | - | SynchronizedSortedBag | SynchronizedSortedBag | Collections.synchronizedSortedMap(TreeMap<String,Integer>) | | Immutable collection | ImmutableMultiset | UnmodifiableBag | UnmodifiableBag | Collections.unmodifiableMap(HashMap<String, Integer)] | | Immutable and sorted | ImmutableSortedMultiset | UnmodifiableSortedBag | UnmodifiableSortedBag | Collections.unmodifiableSortedMap(TreeMap<String, Integer>) | Examples:

1. Using SynchronizedSortedBag from Apache:

// Parse text to separate words
String INPUT_TEXT = "Hello World! Hello All! Hi World!";
// Create Multiset
Bag bag = SynchronizedSortedBag.synchronizedBag(new TreeBag(Arrays.asList(INPUT_TEXT.split(" "))));

// Print count words
System.out.println(bag); // print [1:All!,2:Hello,1:Hi,2:World!]- in natural (alphabet) order
// Print all unique words
System.out.println(bag.uniqueSet());    // print [All!, Hello, Hi, World!]- in natural (alphabet) order
// Print count occurrences of words
System.out.println("Hello = " + bag.getCount("Hello"));    // print 2
System.out.println("World = " + bag.getCount("World!"));    // print 2
System.out.println("All = " + bag.getCount("All!"));    // print 1
System.out.println("Hi = " + bag.getCount("Hi"));    // print 1
System.out.println("Empty = " + bag.getCount("Empty"));    // print 0

// Print count all words
System.out.println(bag.size());    //print 6

// Print count unique words
System.out.println(bag.uniqueSet().size());    //print 4

2. Using TreeBag from Eclipse(GC):

// Parse text to separate words
String INPUT_TEXT = "Hello World! Hello All! Hi World!";
// Create Multiset
MutableSortedBag<String> bag =  TreeBag.newBag(Arrays.asList(INPUT_TEXT.split(" ")));

// Print count words
System.out.println(bag); // print [All!, Hello, Hello, Hi, World!, World!]- in natural order
// Print all unique words
System.out.println(bag.toSortedSet());    // print [All!, Hello, Hi, World!]- in natural order

// Print count occurrences of words
System.out.println("Hello = " + bag.occurrencesOf("Hello"));    // print 2
System.out.println("World = " + bag.occurrencesOf("World!"));    // print 2
System.out.println("All = " + bag.occurrencesOf("All!"));    // print 1
System.out.println("Hi = " + bag.occurrencesOf("Hi"));    // print 1
System.out.println("Empty = " + bag.occurrencesOf("Empty"));    // print 0

// Print count all words
System.out.println(bag.size());    //print 6

// Print count unique words
System.out.println(bag.toSet().size());    //print 4

3. Using LinkedHashMultiset from Guava:

// Parse text to separate words
String INPUT_TEXT = "Hello World! Hello All! Hi World!";
// Create Multiset
Multiset<String> multiset = LinkedHashMultiset.create(Arrays.asList(INPUT_TEXT.split(" ")));

// Print count words
System.out.println(multiset); // print [Hello x 2, World! x 2, All!, Hi]- in predictable iteration order
// Print all unique words
System.out.println(multiset.elementSet());    // print [Hello, World!, All!, Hi] - in predictable iteration order

// Print count occurrences of words
System.out.println("Hello = " + multiset.count("Hello"));    // print 2
System.out.println("World = " + multiset.count("World!"));    // print 2
System.out.println("All = " + multiset.count("All!"));    // print 1
System.out.println("Hi = " + multiset.count("Hi"));    // print 1
System.out.println("Empty = " + multiset.count("Empty"));    // print 0

// Print count all words
System.out.println(multiset.size());    //print 6

// Print count unique words
System.out.println(multiset.elementSet().size());    //print 4

More examples:

I. Apache Collection:

  1. HashBag - order not defined
  2. SynchronizedBag - concurrent and order not defined
  3. SynchronizedSortedBag - - concurrent and sorted order
  4. TreeBag - sorted order

II. GS / Eclipse Collection

  1. MutableBag - order not defined
  2. MutableSortedBag - sorted order

III. Guava

  1. HashMultiset - order not defined