Google Guava’s Multiset Interface in Java

A Multiset is a collection that is similar to Set, but it may have duplicate elements. It supports duplicate elements by maintaining a count of number of times an element appears in the collection. In this post, we will see how to use Multiset provided by Google’s Guava Library.


 

Since JDK doesn’t provides any implementation of the Multiset, programmers often switch to HashMap to store element-count pairs in Java. In the previous post, we have seen how to implement our own Multiset class in Java using a List. Many third party libraries also offers Multiset implementation such as Guava and Apache Commons Collections. In this post, we will discuss common utility methods provided by Guava’s Multiset interface –

 

1. add(), addAll(), contains() and containsAll() methods

We know that if two equal element are added to the java.util.set, the second element will be discarded. For instance,

 
Guava’s Multiset, on the other hand, can store the duplicates. Any subsequent calls to the add() method for the same value will not be discarded as shown below:

 
Below is simple Java program to demonstrate add*() and contains*() methods provided by Guava’s Multiset interface. It uses HashMultiset implementation of Multiset which is backed by a HashMap.

 

Download   Run Code

Output:
[Smith x 4, David, John x 2]
Multiset contains "David"

 

2. Iterate over the Multiset

Guava’s Multiset provides two collection views: elementSet() returns the distinct elements of the multiset, and entrySet() returns Multiset.Entry instances that provides both distinct element and its count.

 
Input Multiset: (for below methods)

[Smith x 4, David, John x 2]

 

2.1. entrySet()

 

Download   Run Code

Output:

Smith occurs 4 times.
David occurs 1 times.
John occurs 2 times.

 

2.2. elementSet()

 

Download   Run Code

Output:

Smith occurs 4 times.
David occurs 1 times.
John occurs 2 times.

 
elementSet() can also be used to determine the count of distinct elements in the multiset. The default size() method of Guava’s multiset returns the total number of elements contained in the multiset. To get the number of distinct elements, consider using elementSet().size().

 

3. Removing values from the Multiset

Guava’s Multiset provides remove(Object) method that removes a single occurrence of the specified element from the multiset. Guava’s Multiset also provides removeAll(Object, int) method that removes specified number of occurrences of the specified element from the multiset.

 

Download   Run Code

Output:

Before: [Tom x 3, Zachary, David x 2, Peter x 4]
After : [Tom x 2]

 

4. Immutable Multiset in Guava

Guava’s Multiset interface has two immutable implementations – ImmutableMultiset and ImmutableSortedMultiset, which should be preferred over the mutable implementations.

 

Download   Run Code

Output:

[Zachary, Grover, Tom x 3, David x 2]
java.lang.UnsupportedOperationException thrown

 
References: Multiset (Guava: Google Core Libraries for Java 23.0 API)

 
Thanks for reading.




Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 





Leave a Reply

Notify of
avatar
wpDiscuz