WHY EQUALS() METHOD MUST BE COMPATIBLE WITH COMPARETO IN JAVA ?

Alex Leblois

Total Post:67

Points:471
Posted by  Alex Leblois
Java 
 1375  View(s)
Ratings:
Rate this:
Why equals() method must be compatible with compareTo in java ?
  1. Mayank Tripathi

    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. Abhishek Maheshwari

    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.


  1. Hubert Jason

    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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!