Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail NNTP-Posting-Date: Sun, 12 Aug 2012 05:46:18 -0500 Date: Sun, 12 Aug 2012 03:46:17 -0700 From: Patricia Shanahan User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20120713 Thunderbird/14.0 MIME-Version: 1.0 Newsgroups: comp.lang.java.programmer Subject: Re: hashCode References: <563f186a-edb3-4311-ae48-3af7decfce2c@googlegroups.com> <6ac6c252-c370-4f74-b29d-ce5368019977@googlegroups.com> In-Reply-To: <6ac6c252-c370-4f74-b29d-ce5368019977@googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: Lines: 41 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 70.230.200.172 X-Trace: sv3-zyMOL+LhaX12LtwUHHIf1N8tqH9cYtJ7LheYPcFqRIQkWI079ujIOlxUgDKeRCj5tVeEVMrEY8VQxIS!CpeZX7q2XXwFh7x4UiEJCh+OjPCNiir0qCTQQ28/JRLxoynzUE+3Uq2TwjSusQv4heUzfpGTPMJN!tGx66+0bQIhUr7YdSGS4Fif2Se7qStwGw2d0U+mHI8pPJhs= X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 3159 Xref: csiph.com comp.lang.java.programmer:17732 On 8/10/2012 3:22 PM, bob smith wrote: ... > Better in the sense that you would never HAVE to override hashCode. > > Now, there are cases where you HAVE to override it, or your code is very broken. > I've decided to go back to this message, because I feel the key issue is the circumstances under which hashCode would need to be overridden if Object's version returned a constant, compared to the current situation. If Object's hashCode returned a constant, in practice anyone using an object as a key in a hash structure would want it overridden with one that has at least some chance of using multiple buckets. Without that property, a HashMap is an over-complicated, space-wasting cousin of a linked list. The problem with this is that the programmer who knows that Widget instances are going to be used as HashMap keys does not necessarily control the Widget implementation. The programmer writing Widget has no idea whether it will ever be used as a HashMap key, and therefore no way of knowing whether it is safe, assuming Widget inherits the Object equals, to also inherit the Object hashCode. Now compare to the current situation. The programmer implementing Widget decides whether to inherit a superclass equals or to write a Widget-specific equals. That programmer can assume the superclass has a hashCode that would be effective for a HashMap key, and only has to override hashCode if they are overriding equals. In practice, it is a long time since I've written a hashCode manually. Generally, when I decide to override equals I tell Eclipse to generate an equals/hashCode pair based on the fields that control whether two instances are equal. Overriding hashCode is no additional work given that I would be telling Eclipse to generate an equals based on those fields anyway. To me, the current situation seems "better". Patricia