问题
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:
- A change to the return type of one method (
TreeMultiset.entrySet
) - An new interface to match the existing functionality of
TreeMultiset
- A new pair of methods to sum the counts in branches of the tree
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/1331174/why-no-sortedmultiset-in-google-collections