harmony-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paulex Yang <paulex.y...@gmail.com>
Subject [classlib]bug-to-bug compatibility: HashMap
Date Wed, 15 Mar 2006 09:10:39 GMT
Pls. try the test case on HashMap below.

On RI, it print out:
null
null

On Harmony, it print out
null
value1

it is definitely bad practice to reuse a object as hash key by modify 
its hashcode, but I DID see some similar cases before, after all, you 
cannot force but only can *suggest* developers what to do.

So, what should we do?  Try to replicate RI's behavior?


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);
  //both RI and Harmony get null here
  System.out.println(map.get(k));
 
  k.setKey(17);
  //RI get null
  //Harmony get "value1"
  //the number of 17 is *magic* because the default length of HashMap is 16.
  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;
 }
}


-- 
Paulex Yang
China Software Development Lab
IBM



Mime
View raw message