Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4491429

HashMap regression with key value null

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P1 P1
    • 1.4.0
    • 1.4.0
    • core-libs
    • 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.

            mmcclosksunw Michael Mccloskey (Inactive)
            shickeysunw Shannon Hickey (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: