Why equals() method must be compatible with compareTo in java ?

Total Post:67

Points:471
Java 
 1690  View(s)
Ratings:
Rate this:
Why equals() method must be compatible with compareTo in java ?
  1. Post:397

    Points:3117
    Re: Why equals() method must be compatible with compareTo in java ?

    compareTo method in Java must be compatible with equals method in Java i.e. if two objects are equal via equals method compareTo method must return “0” for them, failing this may result in some subtle bug when you store those objects in collection class like arraylist in java. 

  1. Post:1

    Points:7
    Re: Why equals() method must be compatible with compareTo in java ?

    CompareTo() method in Java must be compatible with equals method in Java i.e. if two objects are equal via equals method compareTo method must return “0″ for them, failing this may result in some subtle bug when you store those objects in collection class like arraylist in java.


      Modified On Apr-11-2018 05:23:38 AM
  1. Post:6

    Points:42
    Re: Why equals() method must be compatible with compareTo in java ?

    I think this link will clear the things

    http://www.leepoint.net/data/expressions/22compareobjects.html

      Modified On Apr-11-2018 05:23:50 AM

Answer