是否有一个队列(PriorityQueue)实现也是一个集合?
我正在寻找 PriorityQueue实现也是一个 Set。
compareTo
实现如果其元素不能有与 equals
实现一致的要求。
java 有没有这样的实现?
更新:我现在使用 SortedSet 作为内部集合来实现它。所以我只需要实现缺少的方法来满足队列接口。我还忘记提及,它也必须是一个有界队列,因此它具有容量,并在达到容量时丢弃集合的最后一个元素。
I'm looking for a PriorityQueue implementation which is also a Set.
The compareTo
implementation if its elements must not have the requirement to be consistent with the implementation of equals
.
Is there somewhere such a implementation for java?
Update: I implemented it now using a SortedSet as the internal collection. So I only had to implement the missing methods to satisfy the queue interface. I also forgot to mention that it also had to be a bounded queue, so it features a capacity and discards the last element of the set if capacity is reached.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果拥有一个具有“类似集合”行为的队列就足够了,而且您只是不想接受重复的条目,那么我认为,一个简单的解决方案可能是子类化
PriorityQueue
并覆盖add()
、addAll()
和offer()
方法,例如:顺便说一句 -
add()
调用offer()
内部,所以也许只需重写offer()
方法并在那里进行检查就足够了。If it's sufficient to have a queue with a 'Set-like' behaviour, iaw you just don't want to accept duplicate entries, then I think, an easy solution could be to subclass
PriorityQueue
and override theadd()
,addAll()
andoffer()
methods like:BTW -
add()
callsoffer()
internally, so maybe it's even enough to just override theoffer()
method and do the check there.TreeSet
是一个提供有序迭代器的集合。它实现了SortedSet
接口,它保证由iterator
方法返回的迭代器将按照其自然顺序(通过Comparable
)确定的升序返回集合的元素,或由创建集合时赋予该集合的比较器确定。TreeSet
is a set that provides an ordered iterator. It implements theSortedSet
interface, which guarantees that the iterator returned by theiterator
method will return the set's elements in ascending order as determined either by their natural ordering (throughComparable
), or as determined by a comparator given to the set at its creation.那么
PriorityQueue
本身将依赖于Comparitor
或项目的自然顺序进行排序,同样,Set
也将依赖于基于自然排序或 Comparitor 函数,所以不,我不认为它作为默认 Java 安装的一部分存在......但是如果速度不成问题,您可能可以很容易地创建一个简单地实现您想要的接口,并使用它们的自然支持等...又名
不幸的是,Java 的 java.util.* 数据集类并不总是最容易扩展的,而不需要重写其代码块。
PriorityQueue
支持是堆排序的元素列表,因此插入新元素然后执行contains(e)
测试将执行 O(n) 搜索,因为排序是基于在队列上,不是在数据值上,如果您包含HashSet
来支持Set
功能,则可以缩短查找时间大量的代价是维护数据集引用两次(请记住,Java 是按值传递的,所有对象都位于堆上)。这应该可以提高大型集合的性能。Well the
PriorityQueue
is going to itself be dependent on theComparitor
or the natural ordering of the items for its sorting, likewise theSet
would be dependent on the natural ordering or theComparitor
function so no I don't think one exists as part of the default Java install...But you could probably create one pretty easily if speed isn't a worry by simply implementing the interfaces you want, and using their natural backings, etc... aka
Unfortunately Java's java.util.* data-set classes aren't always the easiest to extend without rewriting chunks of their code.
The
PriorityQueue
backing is a heap sorted list of elements, so inserting a new element and then doing acontains(e)
test will an O(n) search because sorting is based on the queuing, not on the data value, if you include aHashSet
to support theSet
functionality though, you can improve your lookup time a lot at the expense of maintaining the dataset references twice (remember that Java is pass-by-value and all objects live on the heap). This should improve performance of a large set.PriorityQueue 是一个 AbstractCollection - 它具有与 Set 几乎相同的接口。我确信制作一个将 PriorityQueue 转换为 Set 的包装器会很容易。如果您确实需要强制执行,您可以保留插入元素的侧面哈希表以避免重复。
我不认为 PriorityQueue 要求 CompareTo 与 equals 一致。 PriorityQueue 根本不使用 equals(除了它继承的 AbstractCollection 操作?)。
PriorityQueue is an AbstractCollection - this has an almost identical interface to a Set. I'm sure it would be easy to make a wrapper that converts a PriorityQueue to a Set. You could keep a side hash table of inserted elements to avoid duplicates, if you really need to enforce that.
I don't think PriorityQueue requires compareTo to be consistent with equals. PriorityQueue doesn't use equals at all (except for its inherited AbstractCollection operations?).