简体   繁体   English

Java中的数据结构是否与TreeSet类似但允许重复?

[英]Is there data structure in Java that is like TreeSet but allows duplicates?

I want to keep the ordering and allow duplicates. 我想保留订购并允许重复。 Should I build one for myself? 我应该为自己建一个吗? Or is there some way that I can tweak Comparator (which I think remove() and insert() are using the same)? 或者有什么方法可以调整Comparator(我认为remove()和insert()使用相同的)? Thanks. 谢谢。

I want remove() to be in O(log n) and add() to be O(log n) as well. 我希望remove()在O(log n)中,add()也是O(log n)。

PriorityQueue is one way but its remove() method takes O(n). PriorityQueue是一种方法,但其remove()方法采用O(n)。 If possible, how could I tweak this? 如果可能的话,我怎么能调整这个?

Can you see TreeBag of Apache Commons-Collections ? 你能看到Apache Commons-Collections的 TreeBag吗? This use a TreeMap to provide the data storage, which provides provides guaranteed log(n) time cost for the containsKey , get , put and remove operations. 这使用TreeMap提供数据存储,它为containsKeygetput和remove操作提供了保证log(n)时间成本。

A Bag stores each object in the collection together with a count of occurrences. Bag将每个对象与出现次数一起存储在集合中。

Guava's TreeMultiset , perhaps? 也许是番石榴的 TreeMultiset Unlike Apache Commons, TreeMultiset and the other Multiset s get along with generics and properly obey the Collection contract, which makes it somewhat friendlier to use. 不同于Apache的百科全书, TreeMultiset和其他Multiset小号相处泛型和正确遵守Collection合同,这使得它有点友好的使用。

(Disclosure: I contribute to Guava.) (披露:我向番石榴捐款。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM