Javatpoint Logo
Javatpoint Logo

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.

Java Multimap

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.

Java 8 Multimap

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

MultimapExample.java

Output:

----- Printing Multimap using keySet -----

a: [Andrew]
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.

Syntax:

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:

  • X → 1, 2
  • Y → 3

or

  • X → 1
  • X → 2
  • Y → 3

Java Multimap Interface Methods

Method Description
asMap() It returns a view of this multimap as a Map from each distinct key to the nonempty collection of that key's associated values.
clear() 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.
containsKey(Object key) It returns true if this multimap contains at least one key-value pair with the key.
containsValue(Object value) It Returns true if this multimap contains at least one key-value pair with the value.
entries() It returns a view collection of all key-value pairs contained in this multimap, as Map.Entry instances.
equals(Object obj) 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.
get(K key) It returns a view collection of the values associated with the key in this multimap if any.
hashCode() It returns the hash code for this multimap.
isEmpty() It returns true if this multimap contains no key-value pairs.
keys() It returns a view collection containing the key from each key-value pair in this multimap, without collapsing duplicates.
keySet() 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.
removeAll(Object key) 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.
size() It returns the number of key-value pairs in this multimap.
values() It returns a view collection containing the value from each key-value pair contained in this multimap, without collapsing duplicates (so values().size() == size()).






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA