Re: hashCode
On 8/30/2012 9:43 PM, Eric Sosman 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<Type> {
>>>>>> 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<ToBeHashed> hasher = new Hasher<ToBeHashed>() {
> 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<String> (outside String
> itself) use String's private `hash' element? (And before you say
> "Give String a getHash() method," ponder what hashCode() does.)
That is a very good point.
We could argue that equals should only depends on retrievable
information and so should hash.
But as you point out then there is the issue of caching
of hash values.
Arne