Guava MultiMap has implementations ImmutableListMultimap and ImmutableSetMultimap. Given that I have created an ImmutableListMultimap<String, Anything> instance, how can I convert that into a java.util.Map<String, List<Anything>>?
The asMap() method return a java.util.Map<String, Collection<Anything>>, but that cannot be cast to java.util.Map<String, List<Anything>>.
The best I have now is
final Map<String, Collection<Anything>> listMultimap = multimap.asMap();
// convert to Map<String, List<>>
final Map<String, List<Anything>> listMap = listMultimap.entrySet().stream()
.collect(Collectors.toMap(
Map.Entry::getKey,
e -> (List<Anything>) e.getValue()));
but that does not look ideal. Given that the instance variable is of type ListMultimap, shouldn't there be a convenient method to represent it as a Map<..., List>?
Apache commons-collections ArrayListValuedHashMap has the same issue.
ListMultimap.asMap()docs mentionSo just use one of static helper methods for this in
Multimapswhich: