{"id":22434,"date":"2021-04-26T04:25:15","date_gmt":"2021-04-26T04:25:15","guid":{"rendered":"https:\/\/cnsfly.com\/vytcdc\/?p=22434"},"modified":"2024-09-26T11:23:41","modified_gmt":"2024-09-26T11:23:41","slug":"comparing-objects-with-equals-and-hashcode-method","status":"publish","type":"post","link":"https:\/\/cnsfly.com\/vytcdc\/comparing-objects-with-equals-and-hashcode-method\/","title":{"rendered":"Comparing Objects with Equals and Hashcode method"},"content":{"rendered":"<p>Comparing Objects with Equals and Hashcode method<br \/>\nWe\u2019ll introduce two methods that closely belong together: equals() and hashCode(). We\u2019ll focus on their relationship with each other, how to correctly override them, and why we should override both or neither.<\/p>\n<p>The Object class defines both the equals() and hashCode() methods \u2013 which means that these two methods are implicitly defined in every Java class, including the ones we create:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">public class Student {\r\n\r\nprivate int rollno;\r\n\r\nprivate String name;\r\n\r\n public Student(int rollno, String name) \r\n\r\n  {\r\n\r\n    this.rollno = rollno;\r\n\r\n    this.name = name;\r\n\r\n  }\r\n\r\n@Override\r\n\r\n public String toString() {\r\n\r\n    return \u201cStudent [rollno=\u201d + rollno + \u201c, name=\u201d + name + \u201c]\u201d;\r\n\r\n  }\r\n\r\n}\r\n\r\npublic class HashMapDemo {\r\n\r\n  public static void main(String[] args) {\r\n\r\n    HashSet&lt;Student&gt; stu = new HashSet&lt;&gt;(); \r\n\r\n    stu.add(new Student(100,\u201dAbdul\u201d));\r\n\r\n    stu.add(new Student(101,\u201dRaju\u201d));\r\n\r\n    stu.add(new Student(102,\u201dAtish\u201d));\r\n\r\n    stu.add(new Student(100,\u201dAbdul\u201d));\r\n\r\n    stu.add(new Student(104,\u201dJenish\u201d));\r\n\r\n    for(Student s:stu)\r\n\r\n    {\r\n\r\n      System.out.println(s);\r\n\r\n    } \r\n\r\n  }\r\n\r\n}<\/pre>\n<p>Result : still we found duplicate<\/p>\n<p><strong>Student [rollno=102, name=Atish]<\/strong><\/p>\n<p><strong>Student [rollno=104, name=Jenish]<\/strong><\/p>\n<p><strong>Student [rollno=100, name=Abdul]<\/strong><\/p>\n<p><strong>Student [rollno=101, name=Raju]<\/strong><\/p>\n<p><strong>Student [rollno=100, name=Abdul]<\/strong><\/p>\n<p>In the above result still we get duplicate, even we use HashSet Mechanism, in that scenario we need to override equals and hashcode method<\/p>\n<ol>\n<li>Equals method what it does is, it will do field to field comparison<\/li>\n<li>Hashcode method what it does is, it will give same hash code to the object whose value in the object are same.<\/li>\n<\/ol>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">public class Student {\r\n\r\nprivate int rollno;\r\n\r\nprivate String name;\r\n\r\npublic Student(int rollno, String name) \r\n\r\n{\r\n\r\n    this.rollno = rollno;\r\n\r\n    this.name = name;\r\n\r\n}\r\n\r\n@Override\r\n\r\npublic int hashCode() {\r\n\r\n    final int prime = 31;\r\n\r\n    int result = 1;\r\n\r\n    result = prime * result + ((name == null) ? 0 : name.hashCode());\r\n\r\n    result = prime * result + rollno;\r\n\r\n    return result;\r\n\r\n}\r\n\r\n@Override\r\n\r\npublic boolean equals(Object obj) {\r\n\r\n    if (this == obj)\r\n\r\n        return true;\r\n\r\n    if (obj == null)\r\n\r\n        return false;\r\n\r\n    if (getClass() != obj.getClass())\r\n\r\n        return false;\r\n\r\n    Student other = (Student) obj;\r\n\r\n    if (name == null) {\r\n\r\n        if (other.name != null)\r\n\r\n            return false;\r\n\r\n    } else if (!name.equals(other.name))\r\n\r\n        return false;\r\n\r\n    if (rollno != other.rollno)\r\n\r\n        return false;\r\n\r\n    return true;\r\n\r\n}\r\n\r\n@Override\r\n\r\npublic String toString() {\r\n\r\n    return \u201cStudent [rollno=\u201d + rollno + \u201c, name=\u201d + name + \u201c]\u201d;\r\n\r\n}\r\n\r\n}\r\n\r\npublic class HashMapDemo {\r\n\r\n public static void main(String[] args) {\r\n\r\n        HashSet&lt;Student&gt; stu = new HashSet&lt;&gt;(); \r\n\r\n        stu.add(new Student(100,\u201dAbdul\u201d));\r\n\r\n        stu.add(new Student(101,\u201dRaju\u201d));\r\n\r\n        stu.add(new Student(102,\u201dAtish\u201d));\r\n\r\n        stu.add(new Student(100,\u201dAbdul\u201d));\r\n\r\n        stu.add(new Student(104,\u201dJenish\u201d));          \r\n\r\n        for(Student s:stu)\r\n\r\n        {\r\n\r\n            System.out.println(s);\r\n\r\n        }   \r\n\r\n    }\r\n\r\n}<\/pre>\n<p>Result \u2013 (After overriding the equals and hashcode method now there is no duplicate object present)<\/p>\n<p><strong>Student [rollno=102, name=Atish]<\/strong><\/p>\n<p><strong>Student [rollno=100, name=Abdul]<\/strong><\/p>\n<p><strong>Student [rollno=104, name=Jenish]<\/strong><\/p>\n<p><strong>Student [rollno=101, name=Raju]<\/strong><\/p>\n<p>Note: As per the Java documentation, both the methods should be overridden to get the complete equality mechanism; using equals() alone is not sufficient. It means, if we override the equals(), we must override the hashcode() method.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We\u2019ll introduce two methods that closely belong together: equals() and hashCode(). We\u2019ll focus on their relationship with each other, how to correctly override them, and why we should override both or neither.<\/p>\n","protected":false},"author":1,"featured_media":23718,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[74],"tags":[25,26],"class_list":["post-22434","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android","tag-design","tag-development"],"_links":{"self":[{"href":"https:\/\/cnsfly.com\/vytcdc\/wp-json\/wp\/v2\/posts\/22434","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cnsfly.com\/vytcdc\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cnsfly.com\/vytcdc\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cnsfly.com\/vytcdc\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cnsfly.com\/vytcdc\/wp-json\/wp\/v2\/comments?post=22434"}],"version-history":[{"count":1,"href":"https:\/\/cnsfly.com\/vytcdc\/wp-json\/wp\/v2\/posts\/22434\/revisions"}],"predecessor-version":[{"id":33849,"href":"https:\/\/cnsfly.com\/vytcdc\/wp-json\/wp\/v2\/posts\/22434\/revisions\/33849"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cnsfly.com\/vytcdc\/wp-json\/wp\/v2\/media\/23718"}],"wp:attachment":[{"href":"https:\/\/cnsfly.com\/vytcdc\/wp-json\/wp\/v2\/media?parent=22434"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cnsfly.com\/vytcdc\/wp-json\/wp\/v2\/categories?post=22434"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cnsfly.com\/vytcdc\/wp-json\/wp\/v2\/tags?post=22434"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}