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 –

 

Download

Output:

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()
USA:2
Mexico:3

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)

 
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