Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 751
emen Avatar asked Jan 29 '26 23:01

emen


2 Answers

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

Documentation

like image 150
Suren Raju Avatar answered Jan 31 '26 12:01

Suren Raju


Assuming your map is declared as follows : MultiMap<String, Integer> map, you can do the following :

for (String key : map.keySet()) {
    int count = map.get(key).size();
}
like image 39
Alexandre FILLATRE Avatar answered Jan 31 '26 12:01

Alexandre FILLATRE



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!