HashMap's behavior is non-compliant with RI when key is reused
--------------------------------------------------------------
Key: HARMONY-206
URL: http://issues.apache.org/jira/browse/HARMONY-206
Project: Harmony
Type: Bug
Components: Classlib
Reporter: Paulex Yang
Priority: Minor
As discussion in mailing list, when hash key is reused by modify its hashcode, Harmony's
HashMap show different behavior with RI. There are some ways to mimic RI's behavior, including
rehash the hash of key, and cache the hash when key/value pair is put into. I will attach
patches soon.
Test case:
public class HashMapTest {
public static void main(String[] args) {
ReusableKey k = new ReusableKey();
HashMap map = new HashMap();
k.setKey(1);
map.put(k, "value1");
k.setKey(18);
System.out.println(map.get(k));
k.setKey(17);
System.out.println(map.get(k));
}
}
class ReusableKey{
private int key = 0;
public void setKey(int key){
this.key = key;
}
public int hashCode(){
return key;
}
public boolean equals(Object o){
if(o == this){
return true;
}
if(!(o instanceof ReusableKey)){
return false;
}
return key == ((ReusableKey)o).key;
}
}
RI 5.0 output:
null
null
Harmony output:
null
value1
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see:
http://www.atlassian.com/software/jira
|