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

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

问题描述

我正在寻找一个java.util.Queue的实现,或者在Google集合中的某个行为类似于Queue的东西,但也要确保队列的每个元素都是唯一的。 (所有进一步插入都没有效果)

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?

现在我使用一个Queue,使用LinkedList实现,并且在插入之前检查唯一性。 (我使用一个地图做这个,添加/删除元素从侧映射前/后queu)。我不喜欢太多。

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.


请注意,如果元素已重新插入,则插入顺序不会受到影响

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

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天全站免登陆