Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Guava MultiSet vs Map?

Tags:

java

guava

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?

like image 741
Adam Lee Avatar asked Jan 14 '12 07:01

Adam Lee


Video Answer


1 Answers

Advantages of a Multiset<E> over a Map<E, Integer>:

  • No special code required when adding an element that is not already in the collection.
  • Methods for handling the count of elements directly: count(E), add(E, int), etc.
  • The intention of the code is clearer. A Multiset<E> obviously maps the elements to their counts. A Map<E, Integer> could map the elements to arbitrary integers.

See also:

Multiset Javadoc

Multiset explained in the Guava Wiki

like image 64
Arend v. Reinersdorff Avatar answered Oct 06 '22 15:10

Arend v. Reinersdorff