Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!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:16 -0600 Organization: Pan-Pacific Grand Prix Lines: 50 Message-ID: References: <563f186a-edb3-4311-ae48-3af7decfce2c@googlegroups.com> <6ac6c252-c370-4f74-b29d-ce5368019977@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="U2FsdGVkX18V5dNSEfOLH/rcO0zLAKh2" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.3 (gnu/linux) Cancel-Lock: sha1:B92mNEWaQo2kKL4JC0gm1zZ+CTM= sha1:XdyWw3s1NW6kqsozCC/+xeUsXpE= Xref: csiph.com comp.lang.java.programmer:18474 Eric Sosman writes: > 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.) I have, and I don't see a problem. String can define hashCode(), and hashCodeIgnoreCase(), and hashCode(Locale), if those turn out to be useful things to do for that particular class (String is an exceptional class in several ways). The objection is to building hashCode() into the Object hierarchy and hard-coding its use in HashMap. A separate interface would allow more flexibility. -- Jim Janney