I’m using a LinkedBlockingQueue to share some object between threads.
The problem is that I can have some duplicates in this queue.
I have tried this solution:
SortedSet<ResultInsert> set = new TreeSet<ResultInsert>(new MyComparator());
set.addAll(bulkInserts);
and implemented:
@Override
public int compare(ResultInsert arg0, ResultInsert arg1) {
}
For some reason it seems that it does not compare each element from my collection with all the elements.
For instance I have 61 elements and it eliminates some duplicate and I have 51 elements, except that between this 51 there are some more duplicates.
I just did a test : for 61 objects it calls compare 342 times, 351 times,.. not every time the same number of times.
I logged all and I don’t get it. It does not compare all.
Anyone any idea, please? Since this morning I’m trying to solve this and I cannot go further.
You can of course make your own subclass which prevents insertions of duplicates, by overriding
putand just ignore the call if the given element exists in the queue.If you really need to use a
Comparator, it could be written like this:Note that adding an element to the queue becomes a linear operation. You would have to maintain a separate data structure (such as a HashSet) if you would like better performance.