Why does Comparator declare equals?

后端 未结 8 1529
无人及你
无人及你 2021-02-05 04:34

The Comparator interface has its own equals() method. Any class will get equals() by default through Object class. What is the need to have equal

相关标签:
8条回答
  • 2021-02-05 04:37

    Read its javadoc. It's there only to explain what equals() must return if you choose to override it in a class implementing Comparator. You might think that no comparator can be equal to any other, but it's not the case. You might think that two comparators are equal if they return the same thing for any arguments, but it's not the case. The javadoc explains that two comparators are equal if they impose the same ordering, whatever the arguments given. The javadoc also says:

    Note that it is always safe not to override Object.equals(Object)

    Most of the time, you don't override equals() in comparators.

    0 讨论(0)
  • 2021-02-05 04:44

    The equals() method in Comparator is provided to force a user implementing the Comparator interface to implement equals() with some additional rules and constraints in addition to the ones already applied on equals() from Object.

    The additional rule being:

    This method must obey the general contract of Object.equals(Object). Additionally, this method can return true only if the specified object is also a comparator and it imposes the same ordering as this comparator. Thus, comp1.equals(comp2) implies that sgn(comp1.compare(o1, o2))==sgn(comp2.compare(o1, o2)) for every object reference o1 and o2.

    0 讨论(0)
  • 2021-02-05 04:46
    Comparator interface have their own equals() method
    

    Well,. first of all, it should be clear that whenever you implement Comparable interface you should provide your program to decide when objects are equal, less or greater.

    I am quite confuse about have equals() inside Comparator. Any class will get equals() by default through Object class.
    

    The equals() method implementation u inherit from Object class only checks whether the two referances point to the same object or not. It dosn't apply any comparison. It's you who will provide in your class (or possibly in your Interface) the criteria for objects to be equal.

    Then what is need to have equals() method inside an interface?
    

    Obviously , whenever you implement when objects are less , greater than you must implement when they are equal. so rather than relying on default Object equals() method you should provide your logic to check equality

    0 讨论(0)
  • 2021-02-05 04:47

    From the Java documentations, the reason why Comparator has it's own equals() method:

    However, overriding this method may, in some cases, improve performance by allowing programs to determine that two distinct comparators impose the same order.

    0 讨论(0)
  • 2021-02-05 04:49

    Comparator refines the contract of Object.equals: It has to satisfy the constraints set out by Object.equals and then some.

    Additionally, this method can return true only if the specified object is also a comparator and it imposes the same ordering as this comparator. Thus, comp1.equals(comp2) implies that sgn(comp1.compare(o1, o2))==sgn(comp2.compare(o1, o2)) for every object reference o1 and o2.

    Declaring an equals inside Comparator allows you to document this in the form of javadoc.

    Note that the documentation of the API also serves as the contract, so it's not just cosmetics here. It's explicit constraints that other code and your code can rely on.

    In similar situations where you have less established methods, it may also serve as documenting an intent. I.e., Interface.method should be there, regardless of how its super interfaces evolves.

    0 讨论(0)
  • 2021-02-05 04:51

    from the docs:

    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.

    0 讨论(0)
提交回复
热议问题