Re: HashMap get/put
Dave Searles wrote:
Patricia Shanahan wrote:
Wojtek wrote:
Lew wrote :
And this is relevant how? 'Map' retrieval is defined in terms of
'equals()', not 'hashCode()'.
The initial lookup up is done with a hashcode.
You could contrive a case where the hashcode is randomly generated
for a given object. It will never be found in the HashMap, yet the
equals would return true.
You could not have a random hashcode that conforms to the Object
hashCode contract, because of the requirement that two objects that are
equal according to their equals methods have the same hashCode result.
Actually, you could if the equals method always returned false. Then
hashCode conforms. But equals may not, if the equals contract requires
that if a == b then a.equals(b). In that case, a random-but-set-at-birth
hashCode and equals equivalent to == is as close as you can get while
conforming to BOTH cotnracts (and java.lang.Object basically is set up
like that).
The equals contract indeed requires it to be reflexive, so an always
false equals would be incorrect.
Patricia