Path: csiph.com!usenet.pasdenom.info!gegeweb.org!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: Jim Janney Newsgroups: comp.lang.java.programmer Subject: Re: hashCode Date: Fri, 31 Aug 2012 09:08:35 -0600 Organization: an undertaking of great advantage, but nobody to know what it is Lines: 56 Message-ID: References: <563f186a-edb3-4311-ae48-3af7decfce2c@googlegroups.com> <502598d0$0$287$14726298@news.sunsite.dk> <5028191b$0$290$14726298@news.sunsite.dk> <50317b03$0$281$14726298@news.sunsite.dk> <503bfd0f$0$282$14726298@news.sunsite.dk> <3zc%r.5945$pd4.2658@newsfe21.iad> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx04.eternal-september.org; posting-host="75975abe3fe3503ca7350803ab98e478"; logging-data="8815"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18Vg5UqgWGSGIiZRaNg8t/y" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.3 (gnu/linux) Cancel-Lock: sha1:lDVQpb+4i3/VtI3eImfHljCS5xA= sha1:OokA667vHcIw70SZQjleuihXBpY= Xref: csiph.com comp.lang.java.programmer:18475 Daniele Futtorovic writes: > On 31/08/2012 03:43, Eric Sosman allegedly wrote: >> On 8/30/2012 6:52 PM, Daniele Futtorovic wrote: >>> On 29/08/2012 20:49, Eric Sosman allegedly wrote: >>>> On 8/29/2012 2:06 PM, Daniel Pitts wrote: >>>>> On 8/28/12 5:02 PM, markspace wrote: >>>>>> On 8/28/2012 4:33 PM, Daniel Pitts wrote: >>>>>>> >>>>>>> interface Hasher { >>>>>>> int hash(Type t); >>>>>> >>>> The difficulty is that an external Hasher would have no access to >>>> private fields (...) >>> >>> This. >> >> ? >> >> package deal; >> public class ToBeHashed { >> private int noGettersForMeThanks; >> ... >> } >> >> package express; >> Hasher hasher = new Hasher() { >> public int hashCode(ToBeHashed obj) { >> // How do you obtain the value of >> // obj.noGettersForMeThanks >> // if that would be useful? >> } >> ... >> } >> >> As an example of why a hasher might want access to a strictly-private >> field, I offered String: How could a Hasher (outside String >> itself) use String's private `hash' element? (And before you say >> "Give String a getHash() method," ponder what hashCode() does.) >> > > Sorry for not being clear, Eric. I meant to emphasize how IMO that one > single point you mention is the decisive argument that settles the issue. > > Imposing that classes should expose all information "relevant" (says > who??) to hashing is utter rubbish IMNSHO. Objects that compare equal must hash to the same value. It follows that if the hash function uses a value, so must the comparison method. Relying on non-public information in the comparison method means allowing two objects to compare non-equal even though there is no way, using public information, to distinguish between them. I can honestly say I've never done that or felt a need to do that. -- Jim Janney