Java 8 Multimap
Java provides various useful built-in collection libraries. But sometimes we required special type of collections that are not in-built in Java's standard library. One of this collection is the Multimap. In this section, we will learn what is multimap and how to implement multimap in Java, and the Multimap interface of the Guava library.
In Java, Map is a data structure that allows us to map key to value. On the other hand, multimap is a new collection type found in the Guava library that allows the mapping of a single key to multiple values (like one-to-many relations in DBMS). But note that JDK does not allow multimapping.
The alternative solution to implement multimap in Java by using Google's Guava library and Apache Commons Collections libraries. Both provides an implementation of the Multimap interface. It can store more than one value against a single key. Both the keys and values stored in the collection and considered as an alternative to Map<K, List<V>> or Map<K, Set<V>> (standard JDK Collections Framework).
But using Multimap of Google's Guava library is not much helpful for us. Instead, we will implement our own Multimap class in Java that can also be customized accordingly. It is easy to write a Multimap class in Java.
The following Java program depicts the implementation of the Multimap class in Java using Map and collection.
Java Multimap Implementation
----- Printing Multimap using keySet -----
b: [Albert, Tom]
d: [Sam, Reo]
g: [Jack, David]
Using Google's Guava Library
Multimap<K, V> interface is defined in com.google.common.collect package of the Guava library. It implements many classes named as follows:
ArrayListMultimap, ForwardingListMultimap, ForwardingMultimap, ForwardingSetMultimap, ForwardingSortedSetMultimap, HashMultimap, ImmutableListMultimap, ImmutableMultimap, ImmutableSetMultimap, LinkedHashMultimap, LinkedListMultimap, TreeMultimap.
A collection that maps keys to values (the same as in Map) but each key may be associated with multiple values. We can visualize the contents of a multimap either as a map from keys to nonempty collections of values. For example:
Java Multimap Interface Methods
||It returns a view of this multimap as a Map from each distinct key to the nonempty collection of that key's associated values.
||It removes all key-value pairs from the multimap, leaving it empty.
|containsEntry(Object key, Object value)
||It returns true if this multimap contains at least one key-value pair with the key and the value.
||It returns true if this multimap contains at least one key-value pair with the key.
||It Returns true if this multimap contains at least one key-value pair with the value.
||It returns a view collection of all key-value pairs contained in this multimap, as Map.Entry instances.
||It compares the specified object with this multimap for equality.
|forEach(BiConsumer<? super K,? super V> action)
||It performs the given action for all key-value pairs contained in this multimap.
||It returns a view collection of the values associated with the key in this multimap if any.
||It returns the hash code for this multimap.
||It returns true if this multimap contains no key-value pairs.
||It returns a view collection containing the key from each key-value pair in this multimap, without collapsing duplicates.
||It returns a view collection of all distinct keys contained in this multimap.
|put(K key, V value)
||Stores a key-value pair in this multimap.
|putAll(K key, Iterable<? extends V> values)
||It stores a key-value pair in this multimap for each of values, all using the same key, key.
|putAll(Multimap<? extends K,? extends V> multimap)
||It stores all key-value pairs of multimap in this multimap, in the order returned by multimap.entries().
|remove(Object key, Object value)
||It removes a single key-value pair with the key and the value from this multimap, if such exists.
||It removes all values associated with the key.
|replaceValues(K key, Iterable<? extends V> values)
||It stores a collection of values with the same key, replacing any existing values for that key.
||It returns the number of key-value pairs in this multimap.
||It returns a view collection containing the value from each key-value pair contained in this multimap, without collapsing duplicates (so values().size() == size()).