是否有一个队列(PriorityQueue)实现也是一个集合?

发布于 2024-08-14 04:27:52 字数 448 浏览 1 评论 0原文

我正在寻找 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心在旅行 2024-08-21 04:27:52

如果拥有一个具有“类似集合”行为的队列就足够了,而且您只是不想接受重复的条目,那么我认为,一个简单的解决方案可能是子类化 PriorityQueue 并覆盖add()addAll()offer() 方法,例如:

@Override
public boolean offer(E e) {
  if (contains(e)) {
    return false; 
  } else {
    return super.offer(e);
  }
}

顺便说一句 - 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 the add(), addAll() and offer() methods like:

@Override
public boolean offer(E e) {
  if (contains(e)) {
    return false; 
  } else {
    return super.offer(e);
  }
}

BTW - add() calls offer() internally, so maybe it's even enough to just override the offer() method and do the check there.

夜吻♂芭芘 2024-08-21 04:27:52

TreeSet 是一个提供有序迭代器的集合。它实现了 SortedSet 接口,它保证由 iterator 方法返回的迭代器将按照其自然顺序(通过 Comparable)确定的升序返回集合的元素,或由创建集合时赋予该集合的比较器确定。

TreeSet is a set that provides an ordered iterator. It implements the SortedSet interface, which guarantees that the iterator returned by the iterator method will return the set's elements in ascending order as determined either by their natural ordering (through Comparable), or as determined by a comparator given to the set at its creation.

失眠症患者 2024-08-21 04:27:52

那么 PriorityQueue 本身将依赖于 Comparitor 或项目的自然顺序进行排序,同样,Set 也将依赖于基于自然排序或 Comparitor 函数,所以不,我不认为它作为默认 Java 安装的一部分存在......

但是如果速度不成问题,您可能可以很容易地创建一个简单地实现您想要的接口,并使用它们的自然支持等...又名

MyQueueSet extends PriorityQueue implements Set {
    HashSet set;
    ...
}

不幸的是,Java 的 java.util.* 数据集类并不总是最容易扩展的,而不需要重写其代码块。

PriorityQueue 支持是堆排序的元素列表,因此插入新元素然后执行 contains(e) 测试将执行 O(n) 搜索,因为排序是基于在队列上,不是在数据值上,如果您包含 HashSet 来支持 Set 功能,则可以缩短查找时间大量的代价是维护数据集引用两次(请记住,Java 是按值传递的,所有对象都位于堆上)。这应该可以提高大型集合的性能。

Well the PriorityQueue is going to itself be dependent on the Comparitor or the natural ordering of the items for its sorting, likewise the Set would be dependent on the natural ordering or the Comparitor 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

MyQueueSet extends PriorityQueue implements Set {
    HashSet set;
    ...
}

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 a contains(e) test will an O(n) search because sorting is based on the queuing, not on the data value, if you include a HashSet to support the Set 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.

ペ泪落弦音 2024-08-21 04:27:52

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?).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文