Shared publicly  - 
Multiset: a collection with order-independent equality that supports duplicates

A multiset (aka, a bag), is a similar to a set, but permits duplicate occurrences of elements. If you have ever used a Map<K, Integer> to keep track of element counts, check out Multiset<E> instead! It extends the Java Collection API with some convenient, new methods:
-Multiset#count(E) returns the total number of occurrences of an element, also called the frequency or multiplicity
-Multiset#entrySet returns a view of the multiset entries (which provides access to both the element and its count)
-Multiset#elementSet returns a set view of the elements (with duplicates collapsed)
-Bulk operations can be performed using Multiset#add(E, int), remove(E, int), or setCount(E, int)

Guava provides several different Multiset implementations:
-HashMultiset: backed by a HashMap; the most popular of the multiset implementations
-LinkedHashMultiset: backed by a LinkedHashMap; iteration order is defined by insertion order; 
-ImmutableMultiset: immutable; iteration order is defined by insertion order; does not permit null elements
-TreeMultiset: backed by a TreeMap; iteration order is defined by natural order or an explicit Comparator
-ImmutableSortedMultiset: backed by a sorted array; iteration order is defined by natural order or an explicit comparator; does not permit null elements;
-ConcurrentHashMultiset: supports concurrent modifications; does not permit null elements
-EnumMultiset: backed by an EnumMap; useful only with Enums

Have you ever used a Multiset before? If so, how has it improved your code?

-+Kurt Alfred Kluever, Guavian Interface Multiset. All Superinterfaces: Collection, Iterable. All Known Subinterfaces: SortedMultiset. All Known Implementing Classes: ConcurrentHashMultiset, EnumMultiset,...
Arturo Quiroz's profile photoSteven Wang's profile photoAlberto N. Russo's profile photoKurt Alfred Kluever's profile photo
Whoops, I forgot to mention the Multisets utility class:

It has unmodifiable multiset wrappers plus a bunch of other utility methods for operating on two multisets (intersections, subsets, removals, retaining, etc).  Check it out :-)
Nobody knows what this is for because the javadoc doesn't even mention histograms. So: histograms, histograms, histograms.
+Brian Slesinsky Arguably, a histogram is inherently range-based; i.e., if i add "42" it would find the 40-49 bucket and increment the value for that bucket.
Hmm. Or maybe put it in the "42" bucket and summarize into ranges at render time? Probably easier with a SortedMultiset.
SortedMultiset was very much built with that use case in mind. ;)
If you have a pretty small set of keys per bucket. I was thinking of things like "response times in nanoseconds", where it's easy to see how wasteful that approach would be. But we digress^2.
Sorry to resurrect this old post, but I just needed to express my dismay that there's apparently no way to predictably iterate over unique elements in a LinkedHashMultiset.  This has broken my heart!
Hi +Louis Wasserman , by unique elements I mean the elements returned by LinkedHashMultiset.elementSet().  (Maybe I should have said "distinct" elements.)
Er, those do come in predictable order, by when their first occurrence was added to the multiset.
That Javadoc is inherited from the upper-level interface.  Yes, the iteration order is as I described.
Brilliant!  Thank you, you've made my day.
Add a comment...