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>
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 - Counting elements of a Multiset

GuavaTester.java

package com.tutorialspoint;

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());
   }
}

Output

Run the GuavaTester and verify the output.

Occurence of 'b' : 5
Total Size : 10

Example - Getting Distinct elements of a Multiset

GuavaTester.java

package com.tutorialspoint;

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");
      
      //get the distinct elements of the multiset as set
      Set<String> set = multiset.elementSet();

      //display the elements of the set
      System.out.print("Set [");
      
      for (String s : set) {
         System.out.print(s + " ");
      }

      System.out.println("]");
      
      //display all the elements of the multiset using iterator
      Iterator<String> iterator  = multiset.iterator();
      System.out.print("MultiSet [");

      while(iterator.hasNext()) {
         System.out.print(iterator.next() + " ");
      }
      
      System.out.println("]");
   }
}

Output

Run the GuavaTester and verify the output.

Set [a b c d ]
MultiSet [a a b b b b b c c d ]

Example - Printing Occurence of elements of a Multiset

GuavaTester.java

package com.tutorialspoint;

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");
      
      //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"));
   }
}

Output

Run the GuavaTester and verify the output.

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
Advertisements