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 7650065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:09:23+00:00 2026-05-31T11:09:23+00:00

When you implement the Comparator interface in Java, is it required to rewrite the

  • 0

When you implement the Comparator interface in Java, is it required to rewrite the equals method? The java compiler does not complain if I do not do that. But is that OK to do?

  • 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-05-31T11:09:26+00:00Added an answer on May 31, 2026 at 11:09 am

    The Comparator<SomeClass> interface is implemented as a separate class to the SomeClass class. There is no need for SomeClass.equals(Object) to be consistent with the comparator. (Indeed, it would pretty much defeat the purpose of having a separate comparator object!)

    On the other hand, if SomeClass implements Comparable<SomeClass> it is strongly recommended that equals(Object) and compare(SomeClass, SomeClass) to be consistent. The javadoc for Comparable says this:

    "It is strongly recommended (though not required) that natural orderings be consistent with equals. This is so because sorted sets (and sorted maps) without explicit comparators behave "strangely" when they are used with elements (or keys) whose natural ordering is inconsistent with equals. In particular, such a sorted set (or sorted map) violates the general contract for set (or map), which is defined in terms of the equals method."

    "For example, if one adds two keys a and b such that (!a.equals(b) && a.compareTo(b) == 0) to a sorted set that does not use an explicit comparator, the second add operation returns false (and the size of the sorted set does not increase) because a and b are equivalent from the sorted set’s perspective."


    The java compiler does not complain if I do not do that.

    The Java compiler cannot determine whether equals and compare are consistent. (Or equals and hashcode for that matter.) It is theoretically impossible to do this in all cases, and beyond the "state of the art" in practice. And a simple test that you’ve overridden equals would give false warnings in some cases.

    You can’t rely on the compiler to point out logic errors in your application.


    If the question is actually about the semantics of Comparable<T>.equals(Object), then the answer is in the javadocs:

    "Note that it is always safe not to override Object.equals(Object). However, overriding this method may, in some cases, improve performance by allowing programs to determine that two distinct comparators impose the same order."


    Ted Hopp commented thus:

    "Actually, a Comparator is useful for sorting; there’s no reason why making it consistent with equals() would "defeat the purpose". Generally, it’s a good idea to make them consistent, although this is not a requirement."

    First, Ted is misconstruing what I wrote. I said:

    "It …" (i.e. a hypothetical requirement of consistency between T.equals(Object) and Comparator.compare(T, T)` "… would defeat the purpose of having a separate comparator"

    Second, it is neither a good idea or a bad idea for comparators to be consistent with equals. It entirely depends on the intended semantics of equals, and the intended purpose of the comparator.

    • If the comparator is used for ordering elements in a TreeSet, then inconsistency with equals is going to result in strange behavior.

    • If the comparator is used for ordering a list of objects for display purposes, then consistency with equals is likely to be irrelevant.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The Version class in .Net does not implement the CompareTo interface as I would
I'm trying to declare an interface that contains a method which will return a
I am trying to implement generics in Java using Comparable<T> interface. public static <T>
I have class, that contains different types and implements Iterable<Object> interface. But it is
The Java documentation for Comparable recommends that compareTo be consistent with equals (because of
Does any Java Interface implicitly implements java.lang.Object? This question arose when I did something
I learned about the Comparable interface, for which a class must implement compareTo method.
Task: implement paging of database records suitable for different RDBMS. Method should work for
I'm about to implement a feature in our application that allows the user to
I am reading Effective Java. In a section that talks about using function objects

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.