views:

191

answers:

2

Google Collections contains the Multiset interface and the TreeMultiset class, but I was surprised to find that there is no corresponding SortedMultiset interface.

Something like that would be very useful for modelling discrete probability distributions.

Before I attempt to implement it myself, I would like to know if there is a specific reason for leaving it out, e.g. likely violation of Multiset or Collection invariants, or inherent performance problems etc.


Edit: I didn't realise it originally but this is actually 3 separate requests:

  1. A change to the return type of one method (TreeMultiset.entrySet)
  2. An new interface to match the existing functionality of TreeMultiset
  3. A new pair of methods to sum the counts in branches of the tree
+6  A: 

I think it's just that no one's ever needed it yet, so we haven't written it yet. It's something I'd consider.

Kevin Bourrillion
A: 

TreeMultiset.elementSet() returns a SortedSet, which might provide some of the functionality you want.

ETA: finnw, the SortedMultiset methods you're requesting wouldn't provide a significantly faster answer to the question "how many elements in my Multiset are less than 42?" The TreeMultiset implementation would still have to iterate across the multiset entries and sum the counts of the relevant elements.

Jared Levy
Almost. One thing it can't do efficiently is answer "how many elements in my Multiset<Integer> are less than 42?" The element set (and its headSet/tailSet methods) will give you the number of distinct values less than 42, but not the number of elements.
finnw