group-icon
Java Developers
Group for Java developers and programmers. Ask questions and get answers related to Java programming
270 Members
Join this group to post and comment.
Soubhik Maji
Soubhik Maji
Computer Science
15 Jan 2019

Hashcode and Equals Method plus comparable and comparator interface

Hashcode and equals() method 


For the elements to be equal 


I. First two elements hashcode will be checked if its same then 


Ii.equals will be checked ,if it's also same ,they are equal 


Contract:If you override one you must override other 

-----_---------------------------------------------7-----------------------------------------

Comparable and comparator


-every class should implement comparable interface for it to be get sorted by inbuilt sort function


-the method name is compareTo(classname ob1)-the first object is this and second is ob1 .. Return -1 if firstsecond 



Comparator- you can pass this in sort function for classes you don't have access (classes that dont implement comparable)to - here method public int  compare(o T1,o T2); we create a instance of this interface using anonymous class and pass it in sort function