Why does Comparator declare equals?

后端 未结 8 1552
无人及你
无人及你 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: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

提交回复
热议问题