{"id":2811,"date":"2018-04-10T12:03:28","date_gmt":"2018-04-10T12:03:28","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=2811"},"modified":"2018-04-10T12:04:29","modified_gmt":"2018-04-10T12:04:29","slug":"everything-about-equals-method","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/everything-about-equals-method\/","title":{"rendered":"everything about Equals Method"},"content":{"rendered":"<p><strong>There could not be any class which conform to both Same type comparison and Mixed type comparison<\/strong> <\/p>\n<p><strong> Same-Type Comparison<\/strong><br \/>\nWith such an implementation of equals() you can store an Employee(&#8220;Hanni Hanuta&#8221;) and a Student(&#8220;Hanni Hanuta&#8221;) into the same HashSet , but retrieval from the collection will not work as expected. You will not find any of these two contained objects when you ask the HashSet whether it contains a Person(&#8220;Hanni Hanuta&#8221;) , because all three object are unequal to each other. <\/p>\n<p><strong> Mixed-Type Comparison<\/strong><br \/>\nIn a class hierarchy, where Employee and Student are subclasses of a Person , representing roles of a person, it may make sense to compare an Employee to a Student to see whether they are the same Person . With an implementation of equals() that only allows same-type comparison an Employee and a Student would not be comparable.So mixed type comparison allows comparison between parent and child object.With this type of equals() implementation you will have problems storing an Employee(&#8220;Hanni Hanuta&#8221;) and a Student(&#8220;Hanni Hanuta&#8221;) in the same HashSet . The HashSet will reject the second add() operation, because the collection already contains an element that compares equal to the new element.<\/p>\n<p>Lets see an example of Mixed-Type comparison<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nclass BaseClass {\r\n    private int field1 = 0;\r\n\r\n    @Override\r\n    public boolean equals(Object obj) {\r\n        if (obj instanceof BaseClass) {\r\n            return field1 == ((BaseClass) obj).field1;\r\n        }\r\n        return false;\r\n    }\r\n}\r\n\r\nclass BadSubClass extends BaseClass {\r\n    private int field2 = 0;\r\n\r\n    @Override\r\n    public boolean equals(Object obj) {\r\n        if (obj instanceof BadSubClass) {\r\n            return super.equals(obj) \r\n                    &amp;&amp; field2 == ((BadSubClass) obj).field2;\r\n        }\r\n        return false;\r\n    }\r\n}\r\n<\/pre>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nBaseClass baseClass = new BaseClass();\r\nBadSubClass subClass = new BadSubClass();\r\n\r\nSystem.out.println(baseClass.equals(subClass)); \/\/ prints 'true'\r\nSystem.out.println(subClass.equals(baseClass)); \/\/ prints 'false'\r\n<\/pre>\n<p><strong>Now the above implementation does not comply to symmetric property of equals<br \/>\nx and y, x.equals(y) should return true if and only if y.equals(x) returns true.<\/strong><\/p>\n<p>The work around for this is <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nclass BaseClass {\r\n    private int field1 = 0;\r\n\r\n    @Override\r\n    public boolean equals(Object obj) {\r\n        if (obj != null &amp;&amp; obj.getClass() == getClass()) {\r\n            return field1 == ((BaseClass) obj).field1;\r\n        }\r\n        return false;\r\n    }\r\n}\r\n\r\nclass GoodSubClass extends BaseClass {\r\n    private int field2 = 0;\r\n\r\n    @Override\r\n    public boolean equals(Object obj) {\r\n        if (obj instanceof GoodSubClass) {\r\n            return super.equals(obj) &amp;&amp; field2 == ((GoodSubClass) obj).field2;\r\n        }\r\n        return false;\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>There could not be any class which conform to both Same type comparison and Mixed type comparison Same-Type Comparison With such an implementation of equals() you can store an Employee(&#8220;Hanni Hanuta&#8221;) and a Student(&#8220;Hanni Hanuta&#8221;) into the same HashSet , but retrieval from the collection will not work as expected. You will not find any&hellip; <a href=\"https:\/\/codethataint.com\/blog\/everything-about-equals-method\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[245],"tags":[],"class_list":["post-2811","post","type-post","status-publish","format-standard","hentry","category-effective-java"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2811","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=2811"}],"version-history":[{"count":2,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2811\/revisions"}],"predecessor-version":[{"id":2814,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2811\/revisions\/2814"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=2811"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=2811"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=2811"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}