A Queue that ensure uniqueness of the elements?

前端 未结 8 1290
甜味超标
甜味超标 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:54

    This is a very good question. There is no existing straightforward solution. I'll dig up some code I wrote a while back that attempted to do this, and come back and edit this answer.

    EDIT: I'm back. Truly, if you don't need concurrency, you are better off maintaining a Queue and Set separately. For what I was doing, concurrency was a goal, but the best solution I could come up with given that constraint was problematic; basically, since it used a ConcurrentHashMap, the more you were removing the "head" element from the queue (a basic thing to do with a queue), the more unbalanced the hash table would become over time. I can still share this code with you, but I doubt you really want it.

    EDIT: For the case where concurrency is required I gave this answer: Concurrent Set Queue

提交回复
热议问题