---
title: "Why equals() method must be compatible with compareTo in java ?"  
description: "Why equals() method must be compatible with compareTo in java ?"  
author: "Anonymous User"  
published: 2015-09-02  
updated: 2016-10-17  
canonical: https://www.mindstick.com/forum/33434/why-equals-method-must-be-compatible-with-compareto-in-java  
category: "java"  
tags: ["java"]  
reading_time: 1 minute  

---

# Why equals() method must be compatible with compareTo in java ?

Why [equals](https://www.mindstick.com/interview/33895/difference-between-and-equals-method-in-c-sharp)() [method](https://www.mindstick.com/forum/166/webservice-method) [must](https://www.mindstick.com/articles/12978/top-reasons-why-every-magento-ecommerce-store-must-opt-for-mobile-app) be compatible with [compareTo](https://www.mindstick.com/articles/1741/comparable-example-in-java) in [java](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming) ?

## Replies

### Reply by Hubert Jason

I think this link will clear the things

http://www.leepoint.net/data/expressions/22compareobjects.html\

### Reply by Abhishek Maheshwari

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.\
For details check equals() method must be compatible with compareTo in java

### Reply by Anonymous User

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.


---

Original Source: https://www.mindstick.com/forum/33434/why-equals-method-must-be-compatible-with-compareto-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
