Guava Multimap – HashMultimap Implementation Example

Guava Multimap – HashMultimap Implementation

The HashMultimap is a variation of a Map in which multiple values or objects are associated with a single key but it does not allow duplicate key/value pairs in the Map. In this example, you will notice that the value of 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 HashMultimap

@GwtCompatible
public interface Multimap<K,V>

#Method and Description
1Map<K,Collection<V>> asMap()
Returns a view of this multimap as a Map from each distinct key to the nonempty collection of that key’s associated values.
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,V> HashMultimap<K,V> create()
Creates a new, empty HashMultimap with the default initial capacities.
7static <K,V> HashMultimap<K,V> create(int expectedKeys, int expectedValuesPerKey)
Constructs an empty HashMultimap with enough capacity to hold the specified numbers of keys and values without rehashing.
8static <K,V> HashMultimap<K,V> create(Multimap<? extends K,? extends V> multimap)
Constructs a HashMultimap 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.
11Set<V> get(K key)
Returns a view collection of the values associated with key in this multimap, if any.
12int hashCode()
Returns the hash code for this multimap.
13boolean isEmpty()
Returns true if this multimap contains no key-value pairs.
14Multiset<K> keys()
Returns a view collection containing the key from each key-value pair in this multimap, without collapsing duplicates.
15Set<K> keySet()
Returns a view collection of all distinct keys contained in this multimap.
16boolean put(K key, V value)
Stores a key-value pair in this multimap.
17boolean 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.
18boolean putAll(Multimap<? extends K,? extends V> multimap)
Stores all key-value pairs of multimap in this multimap, in the order returned by multimap.entries().
19boolean 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.
20Set<V> removeAll(Object keyName)
Removes all values associated with the key keyName.
21Set<V> replaceValues(K key, Iterable<? extends V> values)
Stores a collection of values with the same key, replacing any existing values for that key.
22int size()
Returns the number of key-value pairs in this multimap.
23Collection<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 HashMultimap 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("---myHashMultimap----------");
    logger.info("No duplicates allowed, Not Sorted...");
    Multimap<String,String> myHashMultimap = HashMultimap.create();
    myHashMultimap.put("777", "Amaury Valdes");
    myHashMultimap.put("777", "Walter White");
    myHashMultimap.put("777", "John Smith");
    myHashMultimap.put("777", "Eric Hamlin");
    myHashMultimap.put("777", "Amaury Valdes");
    
    logger.info("myHashMultimap: " + myHashMultimap);
  }
}

Output

22:02:55.609 [main] INFO  com.avaldes.GuavaSetMultiMapExample - ---myHashMultimap----------
22:02:55.614 [main] INFO  com.avaldes.GuavaSetMultiMapExample - No duplicates allowed, Not Sorted...
22:02:55.635 [main] INFO  com.avaldes.GuavaSetMultiMapExample - myHashMultimap: {777=[Amaury Valdes, Walter White, John Smith, Eric Hamlin]}

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

GuavaHashMultimap

Related Posts

Please Share Us on Social Media

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Leave a Reply

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