I have to convert a Guava Multiset<String>
to a Map<String, Integer>
(Key, Count).
I cannot find any utility function for this. Is there anything shorter than my code below for this?
private static Map<String, Integer> multisetToMap(final Multiset<String> multiset) {
Map<String, Integer> result = new HashMap<>();
for(String element: multiset.elementSet()) {
result.put(element, multiset.count(element));
}
return ImmutableMap.copyOf(result);
}
Indeed, there's no hidden Guava feature for this, and you're doing basically the right thing.
I did not spot a feature request (open or otherwise) for doing exactly this either. But if we ever implement this feature request you might have another alternative.