Search code examples
javaguavamultimap

Count occurrences in MultiMap


I have a MultiMap that has multiple similar values:

{Entertainment=[5], Food=[2, 2, 2], Products=[11], Health & Beauty=[3]}

How do I count the total of these keys so that it counts as follows?

Entertainment = 1
Food = 3
Products = 1
Health & Beauty = 1

The number inside the array is the category id, so that Food has 3 occurrences.


Solution

  • Either multimap.get(key).size() or multimap.keys().count(key) should work.

    Documentation