Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8659117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:55:48+00:00 2026-06-12T15:55:48+00:00

I posted some code here which correctly solved a problem the poster had. OP

  • 0

I posted some code here which correctly solved a problem the poster had. OP wanted to remove duplicates and bring certain special items to the top of a list. I used a TreeSet with a special Comparable class which wrapped the Locale they were working with to achieve what they wanted.

I then got to thinking … as you do … that I was eliminating duplicates by returning 0 from the compareTo method, not by returning true from an equals implementation as one would need to do to correctly indicate a duplicate in a Set (from the definition of a Set).

I have no objection to using this technique but am I using what might be considered an undocumented feature? Am I safe to assume that doing this kind of thing going forward will continue to work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:55:49+00:00Added an answer on June 12, 2026 at 3:55 pm

    It seems like this is pretty well documented in JavaDoc of TreeSet (bold mine):

    Note that the ordering maintained by a set (whether or not an explicit comparator is provided) must be consistent with equals if it is to correctly implement the Set interface. (See Comparable or Comparator for a precise definition of consistent with equals.) This is so because the Set interface is defined in terms of the equals operation, but a TreeSet instance performs all element comparisons using its compareTo (or compare) method, so two elements that are deemed equal by this method are, from the standpoint of the set, equal. The behavior of a set is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Set interface.

    Here is an example of the only (?) JDK class that implements Comparable but is not consistent with equals():

    Set<BigDecimal> decimals = new HashSet<BigDecimal>();
    decimals.add(new BigDecimal("42"));
    decimals.add(new BigDecimal("42.0"));
    decimals.add(new BigDecimal("42.00"));
    System.out.println(decimals);
    

    decimals at the end have three values because 42, 42.0 and 42.00 are not equal as far as equals() is concerned. But if you replace HashSet with TreeSet, the resulting set contains only 1 item (42 – that happened to be the first one added) as all of them are considered equal when compared using BigDecimal.compareTo().

    This shows that TreeSet is in a way “broken” when using types not consistent with equals(). It still works properly and all operations are well-defined – it just doesn’t obey the contract of Set class – if two classes are not equal(), they are not considered duplicates.

    See also

    • What does comparison being consistent with equals mean ? What can possibly happen if my class doesn't follow this principle?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently asked a question and posted some code, to which a suggestion was
I come back here because I have some kind of problem. (I posted 2
I'm writing code to use UIImagePickerController. Corey previously posted some nice sample code on
I recently posted a question here about some memory issues I was having. I've
I had this problem a long time ago and recently I got some clues
I am having some sort of problem with encoding in my ASP.NET HTTPHandler, which
UPDATED FOR CLARIFICATION I posted a similar question a while back found here which
Here i have one character arrray in which there are some splitters are there.
I have a similar problem like posted here but instead of obtain the parent
I ported some code from C to C# and I also added some new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.