-
Bug
-
Resolution: Fixed
-
P1
-
1.4.0
-
None
-
beta2
-
generic, x86, sparc
-
generic, linux, solaris_2.6, windows_nt
-
Verified
HashMap no longer correctly handles null as a key value. Consider the following code that stores an object with a key of null:
import java.util.HashMap;
public class HashMapTest {
public static void main(String[] args) {
HashMap hash = new HashMap();
Object key = null;
Object value = new Object();
hash.put(key, value);
System.out.println(hash.get(key));
}
}
When run under 1.3.1, this prints something like "java.lang.Object@720eeb".
When run under the latest promoted build (b75) it prints "null".
HashMap uses a special internal key, NULL_KEY, to represent null as a key value. Whenever put() is called, it calls maskNull() on the input key to convert any null keys to NULL_KEY. The bug is that it forgets to use this masked value when it later calls addEntry() to add the entry to the hashtable. Instead, it is currently using the unmasked key. The method putForCreate() has the same problem.
Please see the suggested fix.
import java.util.HashMap;
public class HashMapTest {
public static void main(String[] args) {
HashMap hash = new HashMap();
Object key = null;
Object value = new Object();
hash.put(key, value);
System.out.println(hash.get(key));
}
}
When run under 1.3.1, this prints something like "java.lang.Object@720eeb".
When run under the latest promoted build (b75) it prints "null".
HashMap uses a special internal key, NULL_KEY, to represent null as a key value. Whenever put() is called, it calls maskNull() on the input key to convert any null keys to NULL_KEY. The bug is that it forgets to use this masked value when it later calls addEntry() to add the entry to the hashtable. Instead, it is currently using the unmasked key. The method putForCreate() has the same problem.
Please see the suggested fix.
- duplicates
-
JDK-4491460 JCK14, api/java_util/HashMap/index.html, 5 tests, sparc, x86, b75, c1, c2
- Closed
-
JDK-4491466 JCK14, api/java_util/HashSet/index.html, 5 tests, sparc, x86, b75, c1, c2
- Closed
-
JDK-4491353 get & containsKey methods in java.util.Map does not work according to spec
- Closed
-
JDK-4491374 java.util.HashSet add(Object o) when null is added twice it is accepted build75
- Closed
-
JDK-4492195 HashSet.contains() doesn`t work with null keys
- Closed
- relates to
-
JDK-4491516 jbuilder4 won't run with merlin-beta, build 75
- Closed
(1 relates to)