Why no SortedMultiset in Google Collections?

旧时模样 提交于 2019-12-22 08:10:52

问题


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

回答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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!