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

HashMap.containsKey() doesn`t work with null keys

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 1.4.0
    • docs
    • sparc
    • solaris_2.6



      Name: auR10023 Date: 08/14/2001



      java.util.HashMap.containsKey doesn`t work with null keys.
      The javadoc for this method says nothing about the difference between
      null and non-null keys.

      javadoc for HashMap:

      ...
      containsKey

      public boolean containsKey(Object key)

           Returns true if this map contains a mapping for the specified key.

           Specified by:
                containsKey in interface Map
           Overrides:
                containsKey in class AbstractMap
           Parameters:
                key - The key whose presence in this map is to be tested
           Returns:
                true if this map contains a mapping for the specified key.
      ...

      But this method returns false for null key contained in the HashMap.

      Here is the example:

      ---------------t.java---------------


      import java.util.*;

      class t {
          public static void main(String args[]) {
              HashMap map = new HashMap();
              
              map.put(null, null);
              map.put("dd", "dd");
              System.out.println( map.containsKey(null));
          }
      }

      #java -version
      java version "1.3.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
      Java HotSpot(TM) Client VM (build 1.3.0-C, interpreted mode)

      #java t
      true

      #java -version
      java version "1.4.0-beta_refresh"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta_refresh-b75)
      Java HotSpot(TM) Client VM (build 1.4.0-beta_refresh-b75, mixed mode)

      #java t
      false

      ======================================================================

            shommel Scott Hommel (Inactive)
            avusunw Avu Avu (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: