Home > DeveloperSection > Forums > Why equals() method must be compatible with compareTo in java ?
Alex Leblois
Alex Leblois

Total Post:67

Points:471
Posted on    September-02-2015 1:04 AM

 Java Java 
Ratings:


 3 Reply(s)
 903  View(s)
Rate this:
Why equals() method must be compatible with compareTo in java ?


Mayank Tripathi
Mayank Tripathi

Total Post:397

Points:3117
Posted on    September-02-2015 6:54 AM

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. 

Abhishek Maheshwari
Abhishek Maheshwari

Total Post:1

Points:7
Posted on    October-13-2016 12:21 AM

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.


Hubert Jason
Hubert Jason

Total Post:6

Points:42
Posted on    October-17-2016 11:26 PM

I think this link will clear the things

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


Don't want to miss updates? Please click the below button!

Follow MindStick