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

I am using MultiMap from Google Guava 12 as follows:

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

after pasting the values ​​into this multimap, I need to return:

Map<Integer, Set<OccupancyType>>

However, when I do this:

return pkgPOP.asMap();

He brings me back

Map<Integer, Collection<OccupancyType>>

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

+5
source share
2 answers

Check out this issue and comment # 2 from Kevin Bourrillion , Guava dev CEO:

Map<K, Collection<V>> Map<K, Set<V>>, . , : ", SetMultimap ." SetMultimap javadoc, .

, :

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

EDIT:

Guava 15.0 , :

Map<Integer, Set<OccupancyType>> mapOfSets = Multimaps.asMap(pkgPOP);
+13

:

. .

Map<K, Set<V>> - , Java. , Multimap Map<K, Collection<V>>, Map<K, Set<V>> Map<K, Collection<V>>, asMap(), Map<K, Set<V>>.

+8

All Articles