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 |
---|---|
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 true if this multimap contains at least one key-value pair with the value valueName. |
6 | Set<E> elementSet() Returns the set of distinct elements contained in this multiset. |
7 | boolean equals(Object object) Compares the specified object with this multiset for equality. |
8 | int hashCode() Returns the hash code for this multiset. |
9 | Iterator<E> iterator() Returns an iterator over the elements in this collection. |
10 | boolean remove(Object element) Removes a single occurrence of the specified element from this multiset, if present. |
11 | int remove(Object element, int occurrences) Removes a number of occurrences of the specified element from this multiset. |
12 | boolean removeAll(Collection<?> c) Removes all of this collection’s elements that are also contained in the specified collection (optional operation). |
13 | boolean retainAll(Collection<?> c) Retains only the elements in this collection that are contained in the specified collection (optional operation). |
14 | int setCount(E element, int count) Adds or removes the necessary occurrences of an element such that the element attains the desired count. |
15 | 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. |
16 | String 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!!!
Related Posts
- Guava Multimap - ArrayListMultimap Implementation Example
In this tutorial we will cover implementing an ArrayListMultimap (which allows duplicate key/value pairs). - Guava Multimap - HashMultimap Implementation Example
In this tutorial we will cover implementing an HashMultimap which does not allow duplicate key/value pairs, using HashMultimap. - Guava Multimap - TreeMultimap Implementation Example
In this tutorial we will cover a implementing an TreeMultimap which performs Sorting according to natural order and does not allow duplicate key/value pairs. - Guava Multiset Example
In this tutorial we will cover a basic introduction the to Guava's Multiset collection using easy to follow example to show you how best to implement this collection in your own code. - Guava BiMap Example (Bi-Directional Map)
In this tutorial we will cover a basic introduction the to Guava's Bidirectional Map (BiMap) using easy to follow example to show you how best to implement this collection in your own code. - Guava Table Example
In this tutorial we will cover a basic introduction the to Guava's Table using easy to follow example to show you how best to implement this collection in your own code. - Guava ClassToInstanceMap Example
In this tutorial we show you how to use Guava's ClassToInstanceMap collection to store class type as the key and the instance of the collection as a value.
Leave a Reply