Guava Multiset Example

Guava Multiset Implementation

A Multiset extends Multimap and has the added feature that it holds duplicate values and maintains their counts. The Multiset is sort of a hybrid between a List and a Set — It allows duplicates but the order of the elements in the set is not guaranteed. Another name for Multiset is a Bag. In this example, you will notice how the values can appear multiple times in the collection. In addition, you will notice how multiset performs element counts for all distinct elements.

Methods available to TreeMultimap

@GwtCompatible
public interface Multiset<E>
extends Collection<E>

#Method and Description
1boolean add(E element)
Adds a single occurrence of the specified element to this multiset.
2int add(E element, int occurrences)
Adds a number of occurrences of an element to this multiset.
3boolean contains(Object element)
Determines whether this multiset contains the specified element.
4boolean containsAll(Collection<?> elements)
Returns true if this multiset contains at least one occurrence of each element in the specified collection.
5int count(Object element)
Returns true if this multimap contains at least one key-value pair with the value valueName.
6Set<E> elementSet()
Returns the set of distinct elements contained in this multiset.
7boolean equals(Object object)
Compares the specified object with this multiset for equality.
8int hashCode()
Returns the hash code for this multiset.
9Iterator<E> iterator()
Returns an iterator over the elements in this collection.
10boolean remove(Object element)
Removes a single occurrence of the specified element from this multiset, if present.
11int remove(Object element, int occurrences)
Removes a number of occurrences of the specified element from this multiset.
12boolean removeAll(Collection<?> c)
Removes all of this collection’s elements that are also contained in the specified collection (optional operation).
13boolean retainAll(Collection<?> c)
Retains only the elements in this collection that are contained in the specified collection (optional operation).
14int setCount(E element, int count)
Adds or removes the necessary occurrences of an element such that the element attains the desired count.
15boolean 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.
16String toString()
Returns a string representation of the object.

Java Multiset Example

package com.avaldes;

import java.util.Set;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.Multiset;

public class GuavaMultiSetExample {
  public static void main(String[] args) {
    Multiset<String> myMultiset = HashMultiset.create();
        
    System.out.println("---myMultiset-----------");
    myMultiset.add("Amaury Valdes");
    myMultiset.add("Walter White");
    myMultiset.add("John Smith");
    myMultiset.add("Eric Hamlin");
    myMultiset.add("Jamie Anderson", 5);
    myMultiset.add("Amaury Valdes");
                
    System.out.format("myMultiset: %s\n", myMultiset);
        
    System.out.format("\nLet's count how many times certain elements appear...\n");
    System.out.format("Amaury Valdes appears %d times in the collection...\n", myMultiset.count("Amaury Valdes"));
    System.out.format("Walter White appears %d times in the collection...\n", myMultiset.count("Walter White"));
    System.out.format("Jamie Anderson appears %d times in the collection...\n", myMultiset.count("Jamie Anderson"));
        
    //---Let's loop through all the distinct elements
    System.out.format("\nLoop through all elements...\n");
    Set<String> set = myMultiset.elementSet();
    for (String s : set) {
      System.out.format("%s appears %d times in the collection...\n", s, myMultiset.count(s));
    }
  }
}

Output

---myMultiset-----------
myMultiset: [Jamie Anderson x 5, Walter White, Amaury Valdes x 2, John Smith, Eric Hamlin]

Let's count how many times certain elements appear...
Amaury Valdes appears 2 times in the collection...
Walter White appears 1 times in the collection...
Jamie Anderson appears 5 times in the collection...

Loop through all elements...
Jamie Anderson appears 5 times in the collection...
Walter White appears 1 times in the collection...
Amaury Valdes appears 2 times in the collection...
John Smith appears 1 times in the collection...
Eric Hamlin appears 1 times in the collection...

That’s It!

I hope you enjoyed this tutorial. Please continue to share the love and like us so that we can continue bringing you quality tutorials. Happy Guava Coding!!!

GuavaMultiset

Related Posts

Please Share Us on Social Media

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *