{"id":145,"date":"2015-09-02T21:26:46","date_gmt":"2015-09-03T04:26:46","guid":{"rendered":"http:\/\/willclausen.com\/?p=145"},"modified":"2015-09-03T09:23:00","modified_gmt":"2015-09-03T16:23:00","slug":"effective-java-item-9","status":"publish","type":"post","link":"http:\/\/willclausen.com\/?p=145","title":{"rendered":"Effective Java: Item #9"},"content":{"rendered":"<p>Header: I am working my way through effective java and am writing these posts to help solidify my understanding of what&#8217;s in the book. This is the first post of it&#8217;s kind, but I&#8217;m sure I&#8217;ll get around to writing posts on the previous 8 items, along with the next 45-ish.<\/p>\n<p><strong>Effective Java: Item #9, Always override <code>.hashcode()<\/code> when overriding <code>.equals()<\/code><\/strong><\/p>\n<p>This item builds off of the last one, which discussed things that must be considered when overriding the <code>.equals()<\/code> function for a class.<\/p>\n<p>When <code>.equals()<\/code> is overridden, you absolutely\u00a0<em>must<\/em>override <code>.hashcode()<\/code>. If you don&#8217;t you will not be able to use hashmaps with instances of your class. This would suck, as hashmaps can help make many important operations\u00a0run in linear instead of quadratic time.<\/p>\n<p>So, it&#8217;s necessary to override <code>.hashcode()<\/code> when overriding <code>.equals()<\/code>. What now? How do we do that? The key is to use the member variables for the class, so that classes with equal data members have equal hash codes.<\/p>\n<p>This item provides some simple tips to create a worthwhile hash function using the type\/value of the member variables in a class.<\/p>\n<p>For each field, <code>f<\/code>, in the class compute a value <code>(int) c<\/code>, where:<\/p>\n<ol>\n<li>If <code>f<\/code> is a boolean, <code>c = ( f &amp;&amp; 1);<\/code><\/li>\n<li>If <code>f<\/code> is an int, <code>c = f;<\/code><\/li>\n<li>if <code>f<\/code> is a long, <code>c = (int) (f ^ (f &gt;&gt;&gt; 32));<\/code><\/li>\n<li>If <code>f<\/code> is a float, <code>c = Float.floatToIntBits(f);<\/code><\/li>\n<li>If <code>f<\/code> is a double <code>c = Double.doubleToLongBits(f);<\/code><\/li>\n<li>If the field is a reference to another object, recursively invoke .hashcode() on it, or compute a &#8220;canonical representation&#8221; for the field and invoke .hashcode() on that.<\/li>\n<\/ol>\n<p>Next, combine <code>c<\/code> into a non-zero <code>result<\/code> variable by <code> result = 31 * result + c<\/code>, and do this for each c corresponding to the fields f<\/p>\n<p>Finally, test whether the function actually produces equal hashcodes for equal objects.<\/p>\n<p>Additionally, you may need to consider the performance of your <code>.hashcode()<\/code> implementation, and if it is too slow, improve the performance by either caching the hash code in the object at construction, or lazily instantiate the hash code.<\/p>\n<p>DONE!<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Header: I am working my way through effective java and am writing these posts to help solidify my understanding of what&#8217;s in the book. This is the first post of it&#8217;s kind, but I&#8217;m sure I&#8217;ll get around to writing posts on the previous 8 items, along with the next 45-ish. Effective Java: Item #9, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45],"tags":[48,49,46,47],"class_list":["post-145","post","type-post","status-publish","format-standard","hentry","category-effective-java","tag-equals","tag-hashcode","tag-effective-java","tag-hash-codes"],"_links":{"self":[{"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/posts\/145","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/willclausen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=145"}],"version-history":[{"count":4,"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/posts\/145\/revisions"}],"predecessor-version":[{"id":149,"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/posts\/145\/revisions\/149"}],"wp:attachment":[{"href":"http:\/\/willclausen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/willclausen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=145"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/willclausen.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}