I got the issue that two integer resolved to the same hashcode in the testcase as below:
public class Test {
private final static Logger log = LoggerFactory.getLogger(Test.class);
private final static LinkedHashMap<Integer,Integer> map = new LinkedHashMap<Integer,Integer>();
/**
* @param args
*/
public static void main(String[] args) {
int j=0;
for(int i=0;i<10000;i++){
++j;
int hash = System.identityHashCode(i);
if(map.containsKey(hash)){
log.info("hashcode of key "+i+" was conflict with "+map.get(hash)+" hashcode was:"+hash);
}else{
map.put(hash, i);
}
}
log.info("length of map:"+map.size()+" expected:"+j);
}
}
The output is as below:
2014-02-08 12:10:59,723 [main] INFO: hashcode of key 1947 was conflict with 422 hashcode was:9578500 <reactive.lib.Test>
2014-02-08 12:10:59,725 [main] INFO: hashcode of key 2246 was conflict with 1966 hashcode was:14850080 <reactive.lib.Test>
2014-02-08 12:10:59,736 [main] INFO: length of map:9998 expected:10000 <reactive.lib.Test>
I expected that all Integer has a unique hashcode - can anyone explain? If it helps, this test was under JDK1.6 on Windows.