Strange Java HashMap behavior - can't find matching object -
When trying to find a key inside
java.util.hashmap >, I face some strange behavior
, and I think I'm missing something, the code segment is basically: hashmap < Key, value & gt; Data = ... key = k1 = ... value v = data.get (k1); Boolean bool1 = Datacontein (K1); For (key 2: data. Cassette ()) {boolean boole 2 = K1. Angle (K2); Boolean bool3 = k2.equals (k1); Boolean bool4 = k1.hashCode () == k2.hashCode (); break; }
This is strange for the loop because specific performance causes me to know that data is only one item at this point And it is
k1
, and in fact the value of bool2
, bool3
and bool4
evaluate true
In that execution, bool1
, however, will be evaluated for false
, and v
will be zero.
Now, this is part of a big program - I could not reproduce the error on a small sample - but even then I think that there is nothing that the rest of the program does That is, this behavior should never happen.
Edit: I have manually verified that to mash that object has been changed between that time when the hash code No changes and that time was examined. I will check this site, but is there any other option?
This behavior can occur if
Example:
public square key {int hashCode = 0}; @ Override Public Ent Hashod () {Return Hashdod; } @ Override Public Boolean Equals (Object OBJE) {If (this == obje) returns is true; If (obj == faucet) return false; If (getClass ()! = Obj.getClass ()) false return; Key other = (key) obj; Return Hashod ==other.hashCode; } Public static zero principal (string [] args throws an exception {hashmap
Comments
Post a Comment