tag:blogger.com,1999:blog-694855878384792308.post6623235597521393353..comments2024-03-15T23:19:22.318-07:00Comments on Java67: How to Override Equals, HashCode and CompareTo method in Javajavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger6125tag:blogger.com,1999:blog-694855878384792308.post-73817115068397223032022-09-27T08:19:56.992-07:002022-09-27T08:19:56.992-07:00EqualsAndHashCodeTest please write whole junit tes...EqualsAndHashCodeTest please write whole junit test case for that class .to better understanding Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-84966995586590724532014-12-17T01:16:08.567-08:002014-12-17T01:16:08.567-08:00As per symmety property of equals() method if x.eq...As per symmety property of equals() method if x.equals(y) is true then y.equals(x) should also be true. If you have used instanceof operator in equals method to check type of object e.g. obj instanceof Type, then it will work for super class but not in sub class because sub instanceof Super is true but super instanceof Sub returns false. I thing following code sample, makes it more clear :<br /><br />import java.util.Date;<br /><br /><br /><br />public class EqualsSymmmetryTest {<br /><br /> <br /> public static void main(String args[]){<br /> <br /> Ticket t = new Ticket(101, new Date() );<br /> MovieTicket m = new MovieTicket(101, new Date());<br /> <br /> System.out.println("t.equals(m) : " + t.equals(m));<br /> System.out.println("m.equals(t) : " + m.equals(t));<br /> <br /> }<br /> <br /> <br />}<br /><br />class Ticket{<br /> <br /> protected int id;<br /> protected Date validity;<br /> <br /> public Ticket(int id, Date validity) {<br /> super();<br /> this.id = id;<br /> this.validity = validity;<br /> }<br /> <br /> @Override<br /> public int hashCode() {<br /> final int prime = 31;<br /> int result = 1;<br /> result = prime * result + id;<br /> result = prime * result<br /> + ((validity == null) ? 0 : validity.hashCode());<br /> return result;<br /> }<br /> <br /> @Override<br /> public boolean equals(Object obj) {<br /> if (this == obj)<br /> return true;<br /> if (obj == null)<br /> return false;<br /> <br /> if (!(obj instanceof Ticket))<br /> return false;<br /><br /> Ticket other = (Ticket) obj;<br /> if (id != other.id)<br /> return false;<br /> if (validity == null) {<br /> if (other.validity != null)<br /> return false;<br /> } else if (!validity.equals(other.validity))<br /> return false;<br /> return true;<br /> }<br /> <br />}<br /><br />class MovieTicket extends Ticket{<br /><br /> public MovieTicket(int id, Date validity) {<br /> super(id, validity);<br /> }<br /> <br /> @Override<br /> public int hashCode() {<br /> final int prime = 31;<br /> int result = 1;<br /> result = prime * result + id;<br /> result = prime * result<br /> + ((validity == null) ? 0 : validity.hashCode());<br /> return result;<br /> }<br /> <br /> @Override<br /> public boolean equals(Object obj) {<br /> if (this == obj)<br /> return true;<br /> if (obj == null)<br /> return false;<br /> <br /> if (!(obj instanceof MovieTicket))<br /> return false;<br /><br /> MovieTicket other = (MovieTicket) obj;<br /> if (id != other.id)<br /> return false;<br /> if (validity == null) {<br /> if (other.validity != null)<br /> return false;<br /> } else if (!validity.equals(other.validity))<br /> return false;<br /> return true;<br /> }<br /> <br />}<br /><br />this will print <br /><br />t.equals(m) : true<br />m.equals(t) : false<br /><br />However, there are cases where you must use instanceof operator instead of getClass() e.g. if you are writing an entity class for Hibernate framework, you must use instanceof because hibernate creates proxies by subclassing your entity class and if you use getClass() to check type, it will return false if compare with an instance of sub class, but instanceof will return true as shown in above example. Another place, where you should use instnaceof instead of getClass() is where you class can be loaded by two differnet class loader e.g. in case of EJB or web application. If same class is loaded by two different class loader then their getClass() would return differnet values. Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-82811294809110122912014-12-11T22:07:18.669-08:002014-12-11T22:07:18.669-08:00But then, look at the next 2 lines:
result = 31*re...But then, look at the next 2 lines:<br />result = 31*result + (name !=null ? name.hashCode() : 0);<br /> => with result == id after first operation => result = 31*id + name.hashCode();<br />next line:<br />result = 31*result + (dob !=null ? dob.hashCode() : 0);<br /> => with result == value from above<br /> => result = 31*31*id + 31*(name.hashCode()) + dob.hashCode()<br />[I left out the 0 if null part just for simplicity, but you see that the 31 DOES play a part.<br /><br />Only case is, if (id == 0 && name == null && dob == null)<br /> => final hashCode == 0.Ajoy Bhatiahttps://www.blogger.com/profile/17192656314049978826noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-90522270367445675202014-10-13T04:45:31.758-07:002014-10-13T04:45:31.758-07:00public int hashCode(){
int result = 0;
...public int hashCode(){<br /> int result = 0;<br /> result = 31*result + id;<br /> result = 31*result + (name !=null ? name.hashCode() : 0);<br /> result = 31*result + (dob !=null ? dob.hashCode() : 0);<br /> <br /> return result;<br /> }<br /><br />in the hash code generation logic prime number plays great role to avoid hash key duplicate, but in the above example some logical issue <br />result = 31 * result + id ===> 0 + id ( because result is initialzed with zero ). <br />prime number not playing any role in this example. <br /><br />ramarkannahttps://www.blogger.com/profile/11673790001924546300noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-53103883286757977272013-11-14T02:58:00.692-08:002013-11-14T02:58:00.692-08:00Can u give one example like how instanceof breaks ...Can u give one example like how instanceof breaks symmetry property of equals method.Unknownhttps://www.blogger.com/profile/11485397649986856048noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-67558101388515835572013-07-16T02:20:56.463-07:002013-07-16T02:20:56.463-07:00Rules to Remember :
1) Always override hashcode i...Rules to Remember :<br /><br />1) Always override hashcode if you are overriding equals and vice-versa.<br /><br />2) Make sure your equals() method is consistent with compare() and compareTo() method, if you intend to use your object with either SortedSet or SortedMap.<br /><br />3) Always use getClass() to check type of object instead of using instanceof operator. Using instanceof, breaks symmetry property of equals method.<br /><br />4) A correct implementation of Equals method must satisfy following condition :<br /><br />a) Don't throw NullPointerException if compared with null.<br /><br />b) Must be symmetric i.e. if x.equals(y) is true and y.equals(x) must be true.<br /><br />c) Must be transitive i.e. if x.equals(y) is true and y.equals(z) is true than x.equals(z) must be true.<br /><br />CheersAnonymousnoreply@blogger.com