确保元素唯一性的队列? [英] A Queue that ensure uniqueness of the elements?

查看:34
本文介绍了确保元素唯一性的队列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找 java.util.Queue 或 Google 集合中行为类似于队列的某些内容的实现,但还要确保队列的每个元素都是唯一的.(所有进一步插入将无效)

I'm looking for a implementation of java.util.Queue or something in the Google collection who behave like a Queue, but also ensure that each element of the queue is unique. (all further insertion will have no effect)

这是可能的,还是我必须手动完成?

It's that possible, or will I have to do it by hand?

现在我正在使用队列和 LinkedList 实现,并在插入前检查唯一性.(我使用侧图来执行此操作,在队列之前/之后从侧图中添加/删除元素).我不太喜欢.

For now I'm using a Queue, with a LinkedList implementation, and I check the uniqueness before insertion. ( I use a side Map for doing this, add / remove element from the side map before / after the queu ). I don't like it too much.

欢迎任何输入.如果它不在 java.util 包中,那么这可能是个坏主意?

Any input is welcome. If it's not in the java.util package, then maybe it's a bad idea?

推荐答案

LinkedHashSet?它的迭代器保留插入顺序,但因为它是一个 Set,所以它的元素是唯一的.

How about a LinkedHashSet? Its iterator preserves insertion order, but because it's a Set, its elements are unique.

正如其文档所说,

请注意,如果元素被重新插入到集合中,则插入顺序不会受到影响.

Note that insertion order is not affected if an element is re-inserted into the set.

为了有效地从这个队列"的头部移除元素,通过它的迭代器:

In order to efficiently remove elements from the head of this "queue", go through its iterator:

Iterator<?> i = queue.iterator();
...
Object next = i.next();
i.remove();

这篇关于确保元素唯一性的队列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆