{"id":232,"date":"2025-06-24T07:32:05","date_gmt":"2025-06-24T07:32:05","guid":{"rendered":"https:\/\/thetestdata.com\/blog\/?p=232"},"modified":"2025-07-16T10:26:12","modified_gmt":"2025-07-16T10:26:12","slug":"why-is-it-important-to-override-hashcode-when-you-override-equals","status":"publish","type":"post","link":"https:\/\/thetestdata.com\/blog\/why-is-it-important-to-override-hashcode-when-you-override-equals\/","title":{"rendered":"Why is it important to override HashCode() when you override equals()?"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Whenever you override <code>equals()<\/code>, <strong>you must also override <code>hashCode()<\/code><\/strong>. Why? Because Java collections like <code>HashMap<\/code>, <code>HashSet<\/code>, and <code>Hashtable<\/code> <strong>rely on both methods<\/strong> to correctly store and retrieve objects.<\/h3>\n\n\n\n<p>\ud83e\udde0 Let\u2019s break it down:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong><code>equals()<\/code><\/strong> checks if two objects are <strong>logically equal<\/strong>.<\/li>\n\n\n\n<li><strong><code>hashCode()<\/code><\/strong> returns an integer (a hash) used to <strong>place the object in hash-based collections<\/strong>.<\/li>\n<\/ol>\n\n\n\n<p>\ud83d\udd17 How they work together:<\/p>\n\n\n\n<p>If two objects are <em>equal<\/em> according to <code>equals()<\/code>, they <strong>must<\/strong> return the same <code>hashCode()<\/code>. If they don&#8217;t, collections like <code>HashMap<\/code> won\u2019t be able to find the object because it&#8217;ll be placed in the wrong &#8220;bucket.&#8221;<\/p>\n\n\n\n<p>\ud83e\uddea Mini Example:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"796\" height=\"448\" src=\"https:\/\/thetestdata.com\/blog\/wp-content\/uploads\/2025\/06\/image-9.png\" alt=\"\" class=\"wp-image-234\" srcset=\"https:\/\/thetestdata.com\/blog\/wp-content\/uploads\/2025\/06\/image-9.png 796w, https:\/\/thetestdata.com\/blog\/wp-content\/uploads\/2025\/06\/image-9-300x169.png 300w, https:\/\/thetestdata.com\/blog\/wp-content\/uploads\/2025\/06\/image-9-768x432.png 768w\" sizes=\"auto, (max-width: 796px) 100vw, 796px\" \/><\/figure>\n\n\n\n<p>Now if you put a <code>Book<\/code> into a <code>HashSet<\/code> and then try to check if it\u2019s in the set with a new but equal <code>Book<\/code> object, it\u2019ll fail\u2014because the <code>hashCode()<\/code> of the new object doesn\u2019t match the original.<\/p>\n\n\n\n<p>\u2705 So, what should you do?<\/p>\n\n\n\n<p>Always override <code>hashCode()<\/code> when you override <code>equals()<\/code> to keep this contract intact:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1009\" height=\"153\" src=\"https:\/\/thetestdata.com\/blog\/wp-content\/uploads\/2025\/06\/image-10.png\" alt=\"\" class=\"wp-image-235\" srcset=\"https:\/\/thetestdata.com\/blog\/wp-content\/uploads\/2025\/06\/image-10.png 1009w, https:\/\/thetestdata.com\/blog\/wp-content\/uploads\/2025\/06\/image-10-300x45.png 300w, https:\/\/thetestdata.com\/blog\/wp-content\/uploads\/2025\/06\/image-10-768x116.png 768w\" sizes=\"auto, (max-width: 1009px) 100vw, 1009px\" \/><\/figure>\n\n\n\n<p>Overriding <code>equals()<\/code> without <code>hashCode()<\/code> is like giving someone your name but not your address\u2014they\u2019ll know who you are, but they\u2019ll never be able to find you.<\/p>\n\n\n\n<p>Want a full example using <code>HashSet<\/code> to see this in action? It\u2019s a classic gotcha moment worth seeing.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn why overriding hashCode() is essential when equals() is overridden in Java. Ensure consistent behavior in hash-based collections like HashMap and HashSet.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"site-container-style":"default","site-container-layout":"default","site-sidebar-layout":"default","disable-article-header":"default","disable-site-header":"default","disable-site-footer":"default","disable-content-area-spacing":"default","footnotes":""},"categories":[17],"tags":[],"class_list":["post-232","post","type-post","status-publish","format-standard","hentry","category-java-interview-questions"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/232","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/comments?post=232"}],"version-history":[{"count":2,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/232\/revisions"}],"predecessor-version":[{"id":451,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/232\/revisions\/451"}],"wp:attachment":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/media?parent=232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/categories?post=232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/tags?post=232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}