Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert MultiMap<Integer, Foo> to Map<Integer, Set<Foo>> using Guava?

Tags:

java

set

guava

I'm using MultiMap from Google Guava 12 like this:

Multimap<Integer, OccupancyType> pkgPOP = HashMultimap.create();

after inserting values into this multimap, I need to return:

Map<Integer, Set<OccupancyType>>

However, when I do:

return pkgPOP.asMap();

It returns me

Map<Integer, Collection<OccupancyType>>

How can I return Map<Integer, Set<OccupancyType>> instead ?

like image 630
brainydexter Avatar asked Jun 26 '12 09:06

brainydexter


People also ask

What is guava Multimap?

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).

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

Look at this issue and comment #2 by Kevin Bourrillion, head Guava dev:

You can double-cast the Map<K, Collection<V>> first to a raw Map and then to the Map<K, Set<V>> that you want. You'll have to suppress an unchecked warning and you should comment at that point, "Safe because SetMultimap guarantees this." I may even update the SetMultimap javadoc to mention this trick.

So do unchecked cast:

@SuppressWarnings("unchecked") // Safe because SetMultimap guarantees this.
final Map<Integer, Set<OccupancyType>> mapOfSets = 
    (Map<Integer, Set<OccupancyType>>) (Map<?, ?>) pkgPOP.asMap();

EDIT:

Since Guava 15.0 you can use helper method to do this in more elegant way:

Map<Integer, Set<OccupancyType>> mapOfSets = Multimaps.asMap(pkgPOP);
like image 118
Xaerxess Avatar answered Oct 02 '22 19:10

Xaerxess


Guava contributor here:

Do the unsafe cast. It'll be safe.

It can't return a Map<K, Set<V>> because of the way Java inheritance works. Essentially, the Multimap supertype has to return a Map<K, Collection<V>>, and because Map<K, Set<V>> isn't a subtype of Map<K, Collection<V>>, you can't override asMap() to return a Map<K, Set<V>>.

like image 28
Louis Wasserman Avatar answered Oct 02 '22 18:10

Louis Wasserman