What is the use of defining equals , compareTo and hashcode methods in a class ? Where are they used ?
Javasearch.buggybread.com

Search Interview Questions


 More than 3000 questions in repository.
 There are more than 900 unanswered questions.
Click here and help us by providing the answer.
 Have a video suggestion.
Click Correct / Improve and please let us know.
Label / Company      Label / Company / Text

   



Java - Interview Questions and Answers

 Q1. What is the use of defining equals , compareTo and hashcode methods in a class ? Where are they used ?Core Java2020-01-09 16:32:57

Ans. equals, compareTo and hashcode are of use when the objects are used within collections.

Equals helps with collections that helps maintaining only unique objects ( like Set )

compare and compareTo helps with collections that helps maintaining objects in order ( TreeSet, TreeMap etc )

hascode helps with collections that facilitates hash searching ( like hashSet, hashMap etc )

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     equals    compareTo   hashcode method


Related Questions

  Can we override compareTo method for Enumerations ?
  How TreeMap orders the elements if the Key is a String ?
  If we add Enum constants to a sorted collection ( Treemap , TreeSet ), What will be the order in which they will be maintained ?
 If compareTo() method returns zero, does it mean that the objects are always equal?
 Difference between equals and compareTo in Java ?



Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked: