Guava Multimap – TreeMultimap Implementation Example

Guava Multimap – TreeMultimap Implementation

The TreeMultimap is a variation of a Map in which multiple values or objects are associated with a single key but it will return a sorted list of objects according to their natural ordering without any duplicate key/value pairs. In this example, you will notice that the values are all sorted in order and Amaury Valdes appears only once in the Multimap element with a key of 777. Notice how duplicates are not allowed and silently fail to be added.

Methods available to TreeMultimap

@GwtCompatible(serializable=true, emulated=true)
public class TreeMultimap<K,V>>
extends Object

#Method and Description
1NavigableMap<K,Collection<V>> asMap()
Returns a map view that associates each key with the corresponding values in the multimap.
2void clear()
Removes all key-value pairs from the multimap, leaving it empty.
3boolean containsEntry(Object key, Object value)
Returns true if this multimap contains at least one key-value pair with the key key and the value value.
4boolean containsKey(Object keyName)
Returns true if this multimap contains at least one key-value pair with the key keyName.
5boolean containsValue(Object valueName)
Returns true if this multimap contains at least one key-value pair with the value valueName.
6static <K extends Comparable,V extends Comparable> TreeMultimap<K,V> create()
Creates an empty TreeMultimap ordered by the natural ordering of its keys and values.
7static <K,V> TreeMultimap<K,V> create(Comparator<? super K> keyComparator, Comparator<? super V> valueComparator)
Creates an empty TreeMultimap instance using explicit comparators.
8static <K extends Comparable,V extends Comparable> TreeMultimap<K,V> create(Multimap<? extends K,? extends V> multimap)
Constructs a TreeMultimap, ordered by the natural ordering of its keys and values, with the same mappings as the specified multimap.
9Set<Map.Entry<K,V>> entries()
Returns a view collection of all key-value pairs contained in this multimap, as Map.Entry instances.
10boolean equals(Object obj)
Compares the specified object with this multimap for equality.
11NavigableSet<V> get(K key)
Returns a collection view of all values associated with a key.
12int hashCode()
Returns the hash code for this multimap.
13boolean isEmpty()
Returns true if this multimap contains no key-value pairs.
14Comparator<? super K> keyComparator()
Returns the comparator that orders the multimap keys.
15Multiset<K> keys()
Returns a view collection containing the key from each key-value pair in this multimap, without collapsing duplicates.
16NavigableSet<K> keySet()
Returns a view collection of all distinct keys contained in this multimap.
17boolean put(K key, V value)
Stores a key-value pair in this multimap.
18boolean putAll(K key, Iterable<? extends V> values)
Stores a key-value pair in this multimap for each of values, all using the same key, key.
19boolean putAll(Multimap<? extends K,? extends V> multimap)
Stores all key-value pairs of multimap in this multimap, in the order returned by multimap.entries().
20boolean remove(Object key, Object value)
Removes a single key-value pair with the key key and the value value from this multimap, if such exists.
21SortedSet<V> removeAll(Object key)
Removes all values associated with the key keyName.
22SortedSet<V> replaceValues(K key, Iterable<? extends V> values)
Stores a collection of values with the same key, replacing any existing values for that key.
23int size()
Returns the number of key-value pairs in this multimap.
24int size()
Returns the number of key-value pairs in this multimap.
25String toString()
Returns a string representation of the multimap, generated by calling toString on the map returned by Multimap.asMap().
26Comparator<? super V> valueComparator()
Returns the comparator that orders the multimap values, with null indicating that natural ordering is used.
27Collection<V> values()
Returns a view collection containing the value from each key-value pair contained in this multimap, without collapsing duplicates (so values().size() == size()).

Java TreeMultimap Example of Guava Multimap

package com.avaldes;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.SortedSetMultimap;
import com.google.common.collect.TreeMultimap;

public class GuavaSetMultiMapExample {
  
  public static void main(String[] args) {
    Logger logger = LoggerFactory.getLogger(GuavaSetMultiMapExample.class);

    logger.info("myTreeMultimap: " + myArrayListMultimap);
    logger.info("No duplicates allowed, Sorted Set...");
    SortedSetMultimap<String,String> myTreeMultimap = TreeMultimap.create();
    myTreeMultimap.put("777", "Amaury Valdes");
    myTreeMultimap.put("777", "Walter White");
    myTreeMultimap.put("777", "Eric Hamlin");
    myTreeMultimap.put("777", "John Smith");
    myTreeMultimap.put("777", "Eric Hamlin");
    
    logger.info("myTreeMultimap: " + myTreeMultimap);
  }
}

Output

22:02:31.310 [main] INFO  com.avaldes.GuavaSetMultiMapExample - ---myTreeMultimap----------
22:02:31.314 [main] INFO  com.avaldes.GuavaSetMultiMapExample - No duplicates allowed, Sorted Set...
22:02:31.329 [main] INFO  com.avaldes.GuavaSetMultiMapExample - myTreeMultimap: {777=[Amaury Valdes, Eric Hamlin, John Smith, Walter White]}

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!!!

GuavaTreeMultimap

Related Posts

Please Share Us on Social Media

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Leave a Reply

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