Java 8 Multimap17 Mar 2025 | 5 min read 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 MultimapIn 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 ImplementationMultimapExample.java Output: ----- Printing Multimap using keySet ----- a: [Andrew] b: [Albert, Tom] d: [Sam, Reo] g: [Jack, David] Using Google's Guava LibraryMultimap<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:
or
Java Multimap Interface Methods
Next TopicParallel Stream in Java |
Millions of gamers across the world have fallen in love with the timeless adventure game Bounce Tales. It provides a fun and immersive gaming experience and was created for Java-capable smartphones. The game is appropriate for gamers of all ages because it incorporates platforming, puzzle-solving, and...
4 min read
In Java, Dictionary is the list of key-value pairs. We can store, retrieve, remove, get, and put values in the dictionary by using the Java Dictionary class. In this section, we will discuss the Java Dictionary class that stores data in key-value pairs just like the...
4 min read
Working with floating-point numbers such as doubles is a common practice in Java. While comparing integers in Java, sometimes be a bit difficult due to their inherent errors. Comparison of integers in Java, is straightforward, compared with double values that requires careful consideration and accounting for...
6 min read
Sun Microsystems created Java in 1995 as a high-level, object-oriented programming language. Java has developed into one of the most well-known high-level languages over time. Nowadays, it is well-liked by businesses in the financial, scientific, and real estate sectors. It is open-source, platform-independent, adaptable, and simple...
6 min read
To extract content from a PDF in Java, there are a number of libraries that are available such as the Apache PDFBox. Apache PDFBox is one of the best libraries that is used to perform various operations on PDFs like PDF to text extraction, PDF...
6 min read
The Java ArrayList class is essentially a resizable array, indicating that its size can change dynamically based on the entries we add or remove. It can be found in the java.util package. The syntax listed below makes it simple to give an ArrayList as an argument...
3 min read
In Java, class loading is an essential method that happens while a category is accessed or referenced in a software. When a Java program begins, the Java Virtual Machine (JVM) locates and loads the important classes into the memory which includes locating the bytecode for...
3 min read
in Java The occurs whenever we are trying to access any item of an array at an index which is not present in the array. In other words, the index may be negative or exceed the size of an array. The is a subclass of...
2 min read
In Java, reducing is a terminal operation that aggregates a stream into a type or a primitive type. Java 8 provides Stream API contains set of predefined reduction operations such as average(), sum(), min(), max(), and count(). These operations return a value by combining the elements...
5 min read
Java 8 was a ground breaking release for the Java platform, introducing a plethora of new features, with lambdas and the Stream API being the most prominent. However, one often overlooked feature is Compact Profiles, which provided a way to scale down the Java Runtime Environment...
3 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India