I have a MultiMap from guava library
Multimap<Integer,String> maps = ArrayListMultimap.create();
maps.put(1, "foo");
maps.put(1, "bar");
maps.put(1, "foo");
maps.put(2, "Hello");
maps.put(2, "foo");
maps.put(2, "World");
maps.put(2, "World");
In this for the key 1, I need to return value which has the highest occurrence. In the above case, it has to return map as
Expected Result:
[1,foo]
[2,World]
I tried
Stream result1 = maps.keySet().stream()
.map(i ->
maps.get(i).stream()
.collect(
Collectors.groupingBy(v -> v, Collectors.counting())
)
);
Result:
{{bar=1, foo=2}=1, {Hello=1, foo=1, World=2}=1}