- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Multiset Interface – Java
Multiset is a collection in Java, that helps in order-independent equality, similar to Set structure. But the only difference is that a multiset can also contain duplicate elements.
- If multiset is visualized as a list, then this wouldn’t be the case since lists can’t hold duplicate values, and list elements are always in a specific order.
- Multiset can be thought of as a collection that lies somewhere between a list and a set structure. In multiset, the duplicates values are allowed, and there is no guarantee that the elements in a multiset would occur in a specific order. Multiset is also known as a ‘bag’.
- It can also be seen as a map that contains elements and their frequencies.
- In a multiset, the total number of occurrences of a specific element is also known as the ‘count’ of that particular element.
- The multiset.count(element) is a function that always returns 0 when the element is not present in the multiset.
- Multiset uses Obejct.equals(java.lang.Object) to check if two instances are considered the same, unless it is specified in the implementation.
- Multiset can be used to add elements, delete them, or specify their frequencies explicitly.
- The setCount(element, 0) function basically suggests that a specific element’s frequency/count be 0, i.e it be deleted or removing all of its occurrences from the multiset.
- Multiset always has elements whose frequencies are positive. No element in a multiset can have a negative frequency. This means an element that had count negative or 0 is considered to be absent from the multiset. They won’t be visible when the elementSet() or entrySet() is called.
- The multiset.size() can be used to obtain the size of the collection, which is same as the sum of the count of all the elements in the multiset.
- To find the number of distinct elements in the multiset, the elementSet().size() is used.
Example
Following is an example −
import com.google.common.collect.HashMultiset; import com.google.common.collect.Multiset; Multiset<String> my_multiset = HashMultiset.create(); my_multiset.add("Sample"); my_multiset.add("Sample"); System.out.println(my_multiset);
Output
[Sample x 2]
A multiset collection of the type string is defined, and elements are added to it using the ‘add’ function. The elements are then printed on the console.
- Related Articles
- Interface in Java
- Java Interface methods
- Marker interface in Java
- Function interface in Java
- Nested interface in Java
- Externalizable Interface in Java
- Deque interface in Java
- BinaryOperator Interface in Java
- IntUnaryOperator Interface in Java
- Why an interface cannot implement another interface in Java?
- Set Interface in Java Programming
- SortedMap Interface in Java Programming
- Marker interface in Java programming.
- Interface enhancements in Java 8
- ConcurrentMap Interface in java\n

Advertisements