I used to meet a bug while using a
TreeSet to save & order some objects. I found some objects are lost although they are totally different with other objects.
According to the official document of JAVA,
TreeSet exactly uses
TreeMap to save elements. Not like
Comparator other than Objects’
equals() to remove duplicates. If
TreeMap don’t add the element into itself.
In this case, my
Comparator compares one integer variable called
size, which only depends on the size of an input object. So that all objects with the same
size value have been defined as the same, only the first one can be kept.
- Update Comparator
- Change to other Sorted Set