Guava Multimap – ArrayListMultimap Implementation Example
Guava Multimap – ArrayListMultimap Implementation
The ArrayListMultimap is a variation of a Map in which multiple values or objects are associated with a single key but it allows duplicate key/value pairs in the Map. In this example, you will notice that the value of Amaury Valdes appears in the Multimap element with a key of 777. Notice how duplicates are allowed when I add another Amaury Valdes instance into the Map.
Methods available to ArrayListMultimap
@GwtCompatible
public interface Multimap<K,V>
# | Method and Description |
---|---|
1 | Map<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. |
2 | void clear() Removes all key-value pairs from the multimap, leaving it empty. |
3 | boolean 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. |
4 | boolean containsKey(Object keyName) Returns true if this multimap contains at least one key-value pair with the key keyName. |
5 | boolean containsValue(Object valueName) Returns true if this multimap contains at least one key-value pair with the value valueName. |
6 | Collection<Map.Entry<K,V>> entries() Returns a view collection of all key-value pairs contained in this multimap, as Map.Entry instances. |
7 | boolean equals(Object obj) Compares the specified object with this multimap for equality. |
8 | Collection<V> get(K key) Returns a view collection of the values associated with key in this multimap, if any. |
9 | int hashCode() Returns the hash code for this multimap. |
10 | boolean isEmpty() Returns true if this multimap contains no key-value pairs. |
11 | Multiset<K> keys() Returns a view collection containing the key from each key-value pair in this multimap, without collapsing duplicates. |
12 | Set<K> keySet() Returns a view collection of all distinct keys contained in this multimap. |
13 | boolean put(K key, V value) Stores a key-value pair in this multimap. |
14 | boolean 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. |
15 | boolean putAll(Multimap<? extends K,? extends V> multimap) Stores all key-value pairs of multimap in this multimap, in the order returned by multimap.entries(). |
16 | boolean 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. |
17 | Collection<V> removeAll(Object keyName) Removes all values associated with the key keyName. |
18 | Collection<V> replaceValues(K key, Iterable<? extends V> values) Stores a collection of values with the same key, replacing any existing values for that key. |
19 | int size() Returns the number of key-value pairs in this multimap. |
20 | Collection<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 ArrayListMultimap 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("---myArrayListMultimap----------"); logger.info("Duplicates allowed, Not Sorted..."); Multimap<String,String> myArrayListMultimap = ArrayListMultimap.create(); myArrayListMultimap.put("777", "Amaury Valdes"); myArrayListMultimap.put("777", "Walter White"); myArrayListMultimap.put("777", "John Smith"); myArrayListMultimap.put("777", "Eric Hamlin"); myArrayListMultimap.put("777", "Amaury Valdes"); } }
Output
21:51:58.529 [main] INFO com.avaldes.GuavaSetMultiMapExample - ---myArrayListMultimap---------- 21:51:58.534 [main] INFO com.avaldes.GuavaSetMultiMapExample - Duplicates allowed, Not Sorted... 21:51:58.543 [main] INFO com.avaldes.GuavaSetMultiMapExample - myTreeMultimap: {777=[Amaury Valdes, Walter White, John Smith, Eric Hamlin, Amaury Valdes]}
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