TreeSet Linker

I used TreeSet with a self-recording comparator. Now, when I add elements to the TreeSet and the Comparator comparison methods return 0, it looks like the TreeSet contains only one of the Object with equal rank.

I have not seen this behavior described in javadocs. Maybe I missed something. Can you confirm this behavior?

I edited the comparator. Now it never returns 0, and the TreeSet contains all objects with equal rank.

Is this the way it should be if I want to have multiple objects with equal rank?

+3
source share
3 answers

This is how it should be, since a collection is defined as including equal objects only once.

0, , (, ) .

+7

, JavaDoc TreeSet:

, , ( , ) equals, Set. (. Comparable Comparator , equals.) , Set equals, , TreeSet compareTo ( compare) , , , , . equals; Set. ( )

+6

, , , TreeMultiset Google , .

+1

Source: https://habr.com/ru/post/1716902/


All Articles