Title: Comparable and Comparator in Java: Java TreeSet Drops
Open Graph Title: Comparable and Comparator in Java: Java TreeSet Drops
Description: TreeSet uses compareTo for equality, not equals(). Two objects returning 0 means one gets dropped silently. Avoid this data loss bug in sorted collections.
Open Graph Description: TreeSet uses compareTo for equality, not equals(). Two objects returning 0 means one gets dropped silently. Avoid this data loss bug in sorted collections.
Opengraph URL: https://thecodeforge.io/java/comparable-comparator-java/
Domain: thecodeforge.io
{
"@context": "https://schema.org",
"@type": "TechArticle",
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "https://thecodeforge.io/java/comparable-comparator-java/"
},
"headline": "Java TreeSet Drops Objects — compareTo Equality Trap",
"description": "TreeSet uses compareTo for equality, not equals(). Two objects returning 0 means one gets dropped silently. Avoid this data loss bug in sorted collections.",
"keywords": "Java TreeSet Drops Objects — compareTo Equality Trap, Java, Intermediate, Collections, TreeSet uses compareTo for equality, not equals(). Two objects returning 0 means one gets dropped silently. Avoid this data loss bug in sorted collections.",
"proficiencyLevel": "Intermediate",
"author": {
"@type": "Person",
"@id": "https://thecodeforge.io/about/#naren",
"name": "Naren",
"url": "https://thecodeforge.io/about/"
},
"publisher": {
"@type": "Organization",
"@id": "https://thecodeforge.io/#org",
"name": "TheCodeForge",
"url": "https://thecodeforge.io",
"logo": {"@type": "ImageObject", "url": "https://thecodeforge.io/assets/og-image.png"}
},
"datePublished": "2026-03-05T00:00:00+00:00",
"dateModified": "2026-08-30T00:00:00+00:00",
"url": "https://thecodeforge.io/java/comparable-comparator-java/",
"inLanguage": "en",
"educationalLevel": "Intermediate",
"image": "https://thecodeforge.io/assets/og-image.png",
"isPartOf": {"@id": "https://thecodeforge.io/#website"}
}
{
"@context": "https://schema.org",
"@type": "BreadcrumbList",
"itemListElement": [
{"@type": "ListItem", "position": 1, "name": "Home", "item": "https://thecodeforge.io/"},
{"@type": "ListItem", "position": 2, "name": "Java", "item": "https://thecodeforge.io/java/"},
{"@type": "ListItem", "position": 3, "name": "Java TreeSet Drops Objects — compareTo Equality Trap", "item": "https://thecodeforge.io/java/comparable-comparator-java/"}
]
}
{
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": [
{
"@type": "Question",
"name": "Can I use both Comparable and Comparator for the same class?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Yes, you can. Comparable defines the natural ordering, and Comparators provide alternative views. They are not mutually exclusive. In fact, it's a common pattern to implement Comparable for the default sort and expose named Comparator constants for specific use cases."
}
},
{
"@type": "Question",
"name": "What happens if I pass a Comparator to TreeSet but the class also implements Comparable?",
"acceptedAnswer": {
"@type": "Answer",
"text": "The Comparator takes precedence. TreeSet will use the provided Comparator for all ordering and equality checks and ignore the natural ordering from Comparable. This allows you to override the default ordering for a particular sorted collection."
}
},
{
"@type": "Question",
"name": "Why does my TreeSet allow duplicate elements?",
"acceptedAnswer": {
"@type": "Answer",
"text": "TreeSet does not allow duplicates according to compareTo. If you see duplicates, it means your compareTo method returns non-zero for objects that you consider duplicates. Check that compareTo returns 0 when you want objects to be considered equal. Alternatively, ensure consistency between compareTo and equals."
}
},
{
"@type": "Question",
"name": "How do I sort by a field that can be null?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Use Comparator.nullsFirst() or nullsLast() to define where nulls should appear in the sorted order. For example: `Comparator.nullsLast(Comparator.comparing(Employee::getSalary))` places employees with null salary at the end of the sorted list."
}
},
{
"@type": "Question",
"name": "Why is it important to have a stable sort for multi-field comparisons?",
"acceptedAnswer": {
"@type": "Answer",
"text": "A stable sort preserves the relative order of elements with equal sort keys. Java's Collections.sort() and Arrays.sort() use a stable sort (TimSort). However, TreeSet is not stable because it uses a tree structure. For multi-field comparisons, if the first field is equal, the second field determines order, so stability is less of a concern. But if you rely on insertion order as a tie-breaker, a stable sort is important."
}
}
]
}
| og:type | website |
| og:image | https://thecodeforge.io/assets/og-image.png |
| og:image:width | 1200 |
| og:image:height | 630 |
Links:
Viewport: width=device-width, initial-scale=1.0
Robots: max-image-preview:large