A Queue that ensure uniqueness of the elements?

前端 未结 8 1293
甜味超标
甜味超标 2020-12-13 08:25

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.

8条回答
  •  既然无缘
    2020-12-13 08:57

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

    As its documentation says,

    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();
    

提交回复
热议问题