Reputation: 25768
My understanding of Multiset is a set with frequency, but I can always use Map to represent the frequency, is there other reason to use Multiset?
Upvotes: 17
Views: 6142
Reputation: 4233
Advantages of a Multiset<E>
over a Map<E, Integer>
:
count(E)
, add(E, int)
, etc.Multiset<E>
obviously maps the elements to their counts. A Map<E, Integer>
could map the elements to arbitrary integers.See also:
Multiset explained in the Guava Wiki
Upvotes: 31
Reputation: 110054
To me, the most important point that sets Multiset
apart from a Map
is that it's a Collection
: you just put stuff into it and you can get counts later. It conceptually fits the use cases for which it's designed where a Map
does not. For those use cases, a Map
is just a hack that kinda-sorta works OK since Java didn't provide anything more appropriate.
Upvotes: 9