
- Guava Tutorial
- Guava - Home
- Guava - Overview
- Guava - Environment Setup
- Guava - Optional Class
- Guava - Preconditions Class
- Guava - Ordering Class
- Guava - Objects Class
- Guava - Range Class
- Guava - Throwables Class
- Guava - Collections Utilities
- Guava - Caching Utilities
- Guava - String Utilities
- Guava - Primitive Utilities
- Guava - Math Utilities
- Guava Useful Resources
- Guava - Quick Guide
- Guava - Useful Resources
- Guava - Discussion
Guava - Multiset Interface
Multiset interface extends ‘Set’ to have duplicate elements, and provides various utility methods to deal with the occurrences of such elements in a set.
Interface Declaration
Following is the declaration for com.google.common.collect.Multiset<E> interface −
@GwtCompatible public interface Multiset<E> extends Collection<E>
Interface Methods
Sr.No | Method & Description |
---|---|
1 |
boolean add(E element) Adds a single occurrence of the specified element to this multiset. |
2 |
int add(E element, int occurrences) Adds a number of occurrences of an element to this multiset. |
3 |
boolean contains(Object element) Determines whether this multiset contains the specified element. |
4 |
boolean containsAll(Collection<?> elements) Returns true if this multiset contains at least one occurrence of each element in the specified collection. |
5 |
int count(Object element) Returns the number of occurrences of an element in this multiset (the count of the element). |
6 |
Set<E> elementSet() Returns the set of distinct elements contained in this multiset. |
7 |
Set<Multiset.Entry<E>> entrySet() Returns a view of the contents of this multiset, grouped into Multiset.Entry instances, each providing an element of the multiset and the count of that element. |
8 |
boolean equals(Object object) Compares the specified object with this multiset for equality. |
9 |
int hashCode() Returns the hash code for this multiset. |
10 |
Iterator<E> iterator() Returns an iterator over the elements in this collection. |
11 |
boolean remove(Object element) Removes a single occurrence of the specified element from this multiset, if present. |
12 |
int remove(Object element, int occurrences) Removes a number of occurrences of the specified element from this multiset. |
13 |
boolean removeAll(Collection<?> c) Removes all of this collection's elements that are also contained in the specified collection (optional operation). |
14 |
boolean retainAll(Collection<?> c) Retains only the elements in this collection that are contained in the specified collection (optional operation). |
15 |
int setCount(E element, int count) Adds or removes the necessary occurrences of an element such that the element attains the desired count. |
16 |
boolean setCount(E element, int oldCount, int newCount) Conditionally sets the count of an element to a new value, as described in setCount(Object, int), provided that the element has the expected current count. |
17 | String toString() Returns a string representation of the object. |
Methods Inherited
This interface inherits methods from the following interface −
- java.util.Collection
Example of Multiset
Create the following java program using any editor of your choice in say C:/> Guava.
GuavaTester.java
import java.util.Iterator; import java.util.Set; import com.google.common.collect.HashMultiset; import com.google.common.collect.Multiset; public class GuavaTester { public static void main(String args[]) { //create a multiset collection Multiset<String> multiset = HashMultiset.create(); multiset.add("a"); multiset.add("b"); multiset.add("c"); multiset.add("d"); multiset.add("a"); multiset.add("b"); multiset.add("c"); multiset.add("b"); multiset.add("b"); multiset.add("b"); //print the occurrence of an element System.out.println("Occurrence of 'b' : "+multiset.count("b")); //print the total size of the multiset System.out.println("Total Size : "+multiset.size()); //get the distinct elements of the multiset as set Set<String> set = multiset.elementSet(); //display the elements of the set System.out.println("Set ["); for (String s : set) { System.out.println(s); } System.out.println("]"); //display all the elements of the multiset using iterator Iterator<String> iterator = multiset.iterator(); System.out.println("MultiSet ["); while(iterator.hasNext()) { System.out.println(iterator.next()); } System.out.println("]"); //display the distinct elements of the multiset with their occurrence count System.out.println("MultiSet ["); for (Multiset.Entry<String> entry : multiset.entrySet()) { System.out.println("Element: " + entry.getElement() + ", Occurrence(s): " + entry.getCount()); } System.out.println("]"); //remove extra occurrences multiset.remove("b",2); //print the occurrence of an element System.out.println("Occurence of 'b' : " + multiset.count("b")); } }
Verify the Result
Compile the class using javac compiler as follows −
C:\Guava>javac GuavaTester.java
Now run the GuavaTester to see the result.
C:\Guava>java GuavaTester
See the result.
Occurence of 'b' : 5 Total Size : 10 Set [ d b c a ] MultiSet [ d b b b b b c c a a ] MultiSet [ Element: d, Occurence(s): 1 Element: b, Occurence(s): 5 Element: c, Occurence(s): 2 Element: a, Occurence(s): 2 ] Occurence of 'b' : 3