Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: Eric Sosman Newsgroups: comp.lang.java.programmer Subject: Re: hashCode Date: Sun, 12 Aug 2012 13:59:07 -0400 Organization: A noiseless patient Spider Lines: 25 Message-ID: References: <563f186a-edb3-4311-ae48-3af7decfce2c@googlegroups.com> <6ac6c252-c370-4f74-b29d-ce5368019977@googlegroups.com> <9K6dneYcKfvgQbrNnZ2dnUVZ_j-dnZ2d@earthlink.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sun, 12 Aug 2012 17:59:16 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="ffb8f7085759b339c1002252b48331a4"; logging-data="19698"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX183XLFHnv4ofgzGfClvJ9xn" User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20120713 Thunderbird/14.0 In-Reply-To: <9K6dneYcKfvgQbrNnZ2dnUVZ_j-dnZ2d@earthlink.com> Cancel-Lock: sha1:fuidOBtkdTqdTIxX1CMLJty+TxM= Xref: csiph.com comp.lang.java.programmer:17737 On 8/12/2012 12:40 PM, Patricia Shanahan wrote: >[...] > I think there are two reasonably usable ways of handling this issue. One > is the current arrangement, in which every class has a hashCode that is > expected to be usable for selecting a hash table bucket. > > Keeping hashCode as an Object method but making it useless for bucket > selection unless overridden would not be a good alternative. > > A more reasonable alternative would be to have hashCode as the only > member of a HashKey interface that would be implemented by every class > whose objects are intended to be suitable for use as has keys. Those > objects that have a hashCode would still have to have a usable one, but > some classes would not implement HashKey and not have a hashCode at all. Ugh. So if J. Random Programmer is too lazy or unimaginative to write hashCode(), that means I can't use his class as a HashMap key, or even put instances in a HashSet? Ugh, again. (And, no: I don't think a HashCalculator interface along the lines of Comparable would save the day.) -- Eric Sosman esosman@ieee-dot-org.invalid