What does the Guava Multimap class provide?
A) A collection that maps keys to multiple values
B) A map that supports concurrent access
C) A thread-safe implementation of HashMap
D) A map that stores values in sorted order
Answer:
A) A collection that maps keys to multiple values
Explanation:
The Guava Multimap
class provides a collection that maps keys to multiple values. Unlike a standard Map
, where each key is associated with a single value, a Multimap
allows a key to be associated with multiple values, making it easier to work with collections where this kind of relationship is needed.
For example:
Multimap<String, String> multimap = ArrayListMultimap.create();
multimap.put("fruit", "apple");
multimap.put("fruit", "banana");
multimap.put("color", "red");
In this example, the key “fruit” is associated with both “apple” and “banana”, while the key “color” is associated with “red”. The Multimap
makes it easy to manage these kinds of associations without manually handling lists or sets of values.