Multiset Interface by Apache Commons Collections in Java
In the previous post, we have seen how to implement Multiset class in Java, which allowed duplicate elements, unlike java.util.Set. This post will discuss about common utility methods provided by Multiset Interface of Apache Commons Collections.
The MultiSet is an interface provided by Apache Commons Collections library. It is similar to a set, but it allows duplicate elements. It supports duplicates by maintaining a count of the total number of occurrences of each element in the collection. Some of the methods provided by the Multiset Interface are:
add(E object):Add one copy of the specified object to the Multiset.add(E object, int occurrences):Adds a number of occurrences of the specified object to the Multiset.remove(Object object):Remove one occurrences of the specified object from the Multiset.remove(Object object, int occurrences):Removes a number of occurrences of the specified object from the Multiset.setCount(E object, int count):Sets the number of occurrences of the specified object in the Multiset to the given count.getCount(Object object):Returns the number of occurrences of the given object currently in the Multiset.entrySet():Returns a Set of all entries contained in the Multiset.uniqueSet():Returns a Set of unique elements in the Multiset.iterator():Returns an iterator over the entire set of elements in the Multiset.
The standard implementation of the Multiset Interface is the HashMultiSet class, which uses a HashMap. Following is a simple Java program to demonstrate various utility methods provided by Apache Commons’s MultiSet interface:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
import org.apache.commons.collections4.MultiSet; import org.apache.commons.collections4.multiset.HashMultiSet; import java.util.ArrayList; import java.util.List; class Main { public static void main(String[] args) { // HashMultiSet is the HashMap-based standard implementation of a multiset MultiSet<String> multiset = new HashMultiSet<>(); /* Add elements using overloaded add() methods */ // add one copy of the specified object to the multiset multiset.add("USA"); // add the total number of occurrences of the specified object to the multiset multiset.add("Japan", 2); multiset.add("China", 3); System.out.println(multiset); // [USA:1, Japan:2, China:3] /* Remove elements using overloaded remove() methods */ // removes one occurrence of the given object from the multiset multiset.remove("China"); multiset.remove("Japan", 1); // remove all occurrences of the specified object from the multiset multiset.remove("China", multiset.getCount("China")); System.out.println(multiset); // [USA:1, Japan:1] /* Update counts of elements */ // equivalent to calling `remove("USA", multiset.getCount("USA")) + add("USA", 2)` multiset.setCount("USA", 2); // equivalent to calling `remove("Japan", multiset.getCount("Japan"))` multiset.setCount("Japan", 0); // equivalent to calling `add("Mexico", 3)` multiset.setCount("Mexico", 3); System.out.println(multiset); // [USA:2, Mexico:3] // Iterate over MultiSet using entrySet() for (MultiSet.Entry<String> entry: multiset.entrySet()) { System.out.println(entry.getElement() + ":" + entry.getCount()); // USA:2, Mexico:3 } // Get Collection of distinct elements System.out.println(multiset.uniqueSet()); // [USA, Mexico] // Get an iterator and insert each element into a list List<String> allElements = new ArrayList<>(); multiset.iterator().forEachRemaining(allElements::add); System.out.println(allElements); // [USA, USA, Mexico, Mexico, Mexico] } } |
That’s all about Multiset Interface by Apache Commons Collections in Java.
Also See:
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)