Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does guava Multimap.values() return a flat collection instead of a collection of collections?

I really like the Multimap class of the google guava library. It is a map type where you can add multiple values for a key, so it effectively maps from a key to a collection of some type. What I especially love is the Multimaps.index() function which takes an Iterable and a key function and returns a Multimap which groups (or indexes or maps) the elements of the Iterable by the value the function returns for each of those elements.

What I find a bit strange is that Multimap.values() returns a flat collection instead of a collection of collections? So the grouping the index function gave me is lost once Ì retrieve the values. I can circumvent that problem by calling Multimap.asMap() and then call values() on that.

Does anyone know why it may make sense that Multimap behaves that way?

like image 505
nansen Avatar asked Mar 28 '12 20:03

nansen


People also ask

What is Multimap guava?

A Multimap is a new collection type that is found in Google's Guava library for Java. A Multimap can store more than one value against a key. Both the keys and the values are stored in a collection, and considered to be alternates for Map<K, List<V>> or Map<K, Set<V>> (standard JDK Collections Framework).

Does Multimap maintain order?

Multimap is similar to a map with the addition that multiple elements can have the same keys. Also, it is NOT required that the key-value and mapped value pair have to be unique in this case. One important thing to note about multimap is that multimap keeps all the keys in sorted order always.

What is set Multimap?

Stores a collection of values with the same key, replacing any existing values for that key. If values is empty, this is equivalent to removeAll(key) . Because a SetMultimap has unique values for a given key, this method returns a Set , instead of the Collection specified in the Multimap interface.

What is MultiHashMap in Java?

MultiHashMap is the default implementation of the MultiMap interface. A MultiMap is a Map with slightly different semantics. Putting a value into the map will add the value to a Collection at that key. Getting a value will return a Collection, holding all the values put to that key.


2 Answers

Multimap.asMap().values() isn't a way around the problem -- it was deliberate that Multimap provides both ways of accessing it, getting a Collection<Collection<V>> via asMap().values() and getting the flattened Collection<V> with values().

More generally speaking, Multimap tries not to just be "a map to collections," but rather "a general way to associate keys with multiple values." So you get the entries() method in addition to values() and keys(). The asMap() view provides a way to treat it as a "map to collections," but that has very different semantics that aren't always what you're looking for.

In any event, the values method is just meant to fill a different niche than the one filled by asMap().values().

like image 188
Louis Wasserman Avatar answered Oct 06 '22 01:10

Louis Wasserman


Does anyone know why it may make sense that Multimap behaves that way?

A Multimap should be viewed as ordinary map, where the keys does not need to be unique.

Key       Val
 a   ->    1
 b   ->    2
 a   ->    3

Values: {1, 2, 3}
like image 22
aioobe Avatar answered Oct 06 '22 00:10

aioobe