Alex Rivera | Logout

How to configure Java Priority Queue to ignore duplicates?

Asked 2012-05-06T10:08:17.027
25

I thought add() is supposed to ignore duplicates, but my output has duplicates. How do I not store duplicates?

I would also like to know how the priority queue checks if two elements are duplicates. I'm guessing it's using the comparator equals, but I just want to be sure.

Thanks

Edit
Report

1 Answer

4

Sets are the only things which ignore duplicates. Lists and queues do not. (LinkedList is a Queue)

If you want to drop duplicates you can check whether the entry you take() is the same as the previous and ignore it. You can do the comparison any way you like. ;)

answered 2012-05-06T10:56:18.393

Your Answer