hiwa said:
I have written an
SSCCE(
http://homepage1.nifty.com/algafield/sscce.html) for your
problem. Please attach your own SSCCE for simplifying the test. JDK
1.6(beta) does not have the problem you have mentioned:
[code snipped]
Your code puts TWO elements in the hashmap. The OP said this bug occurs when
there's only one element in the hashmap. Here's a slightly simplified SSCCE
which demonstrates the problem:
<code>
import java.util.*;
public class NullKey {
public static void main(String[] args) {
TreeMap<Integer, String> tm = new TreeMap<Integer, String>(
new KeyObjComparator());
tm.put(null, "Foo");
// tm.put(Integer.valueOf(10), "ten");
System.out.println(tm.size());
SortedMap sm = tm.headMap(Integer.valueOf(10));
System.out.println(sm.size());
System.out.println(sm.get(null));
}
}
class KeyObjComparator implements Comparator<Integer> {
public int compare(Integer o1, Integer o2) {
int retval;
if (o1 == null) {
if (o2 == null) {
retval = 0;
} else {
retval = -1;
}
} else if (o2 == null) {
retval = 1;
} else {
retval = o1.compareTo(o2);
}
return retval;
}
}
</code>
The output is "1\n0\nFoo\n" when it should be "1\n\1\nFoo\n". This bug
occurs on Java 1.5.0_06-b05
- Oliver