Apache Commons Collections | Multiset Interface

In the previous post, we have seen how to implement our own Multiset class in Java which allowed duplicate elements, unlike java.util.Set. In this post, we will quickly discuss common utility methods provided by Apache Commons’s Multiset interface.


Apache Commons’s MultiSet interface defines a collection that counts the number of times an object appears in the collection. Below is simple Java program to demonstrate various utility methods provided by Apache Commons’s MultiSet Interface –




Multiset after calls to add()      : [USA:1, Japan:2, China:3]
Multiset after calls to remove()   : [USA:1, Japan:1]
Multiset after calls to setCount() : [USA:2, Mexico:3]

Iterating over MultiSet using entrySet()

Returned Set by uniqueSet() : [USA, Mexico]

Printing all elements : [USA, USA, Mexico, Mexico, Mexico]

Also See: Guava’s Multiset

References: MultiSet (Apache Commons Collections 4.1 API)

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)


Thanks for reading.

Please use our online compiler to post code in comments. To contribute, get in touch with us.
Like us? Please spread the word and help us grow. Happy coding 🙂

Leave a Reply

Notify of