-
Bug
-
Resolution: Duplicate
-
P3
-
None
-
1.4.0
-
generic
-
generic
Methods get(Object key) & containsKey(Object key) in interface java.util.Map does not work according to spec when 'null' is passed as key.
According to the documentation , method 'get(Object key)' returns the value to which this map maps the specified key.Returns 'null' if the map contains no mapping for this key.A return value of 'null' doesn't necessarily indicate that the map contains no mapping for the key, it's also possible that the map explicitly maps the key to 'null'.The 'containsKey' operation may be used to distinguish these two cases.
In b75 when 'null' is passed as key, it's not following the spec.(b74 was working ) containsKey(null) is returning false when some value is given for key'null'.
Given below is a testcase which explains this.
TestCase
---------
import java.util.jar.Attributes;
public class GetTests extends Attributes {
public static void main(String args[]) {
GetTests status= new GetTests();
status.getTest02();
}
/**
* tests get with valid arguments after storing a null value
*/
public void getTest02() {
try {
map.put((Attributes.Name)null,"value1");
} catch (NullPointerException e) {
System.out.println("Exception Thrown");
}
map.put(new Attributes.Name("key2"),"value2");
map.put(new Attributes.Name("key3"),"value3");
System.out.println("this.size() :"+this.size());
System.out.println("containsKey : "+containsKey(null) );
System.out.println("get((Attributes.Name)null) :"+map.get((Attributes.Name)null));
}
}
---------------------------------------------------------------
TestResult with b74 :
---------------------
javasoft16:/home/sa122568/Bugs/b75/GetTests 34 % java -version
java version "1.4.0-beta_refresh"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta_refresh-b74)
Java HotSpot(TM) Client VM (build 1.4.0-beta_refresh-b74, mixed mode)
javasoft16:/home/sa122568/Bugs/b75/GetTests 35 % javac *.java
javasoft16:/home/sa122568/Bugs/b75/GetTests 36 % java GetTests
this.size() :3
containsKey : true
get((Attributes.Name)null) :value1
-------------------------------------
TestResults with b75 :
---------------------
javasoft16:/home/sa122568/Bugs/b75/GetTests 41 % 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)
javasoft16:/home/sa122568/Bugs/b75/GetTests 42 % javac *.java
javasoft16:/home/sa122568/Bugs/b75/GetTests 43 % java GetTests
this.size() :3
containsKey : false
get((Attributes.Name)null) :null
------------------------------------------------------------------------
According to the documentation , method 'get(Object key)' returns the value to which this map maps the specified key.Returns 'null' if the map contains no mapping for this key.A return value of 'null' doesn't necessarily indicate that the map contains no mapping for the key, it's also possible that the map explicitly maps the key to 'null'.The 'containsKey' operation may be used to distinguish these two cases.
In b75 when 'null' is passed as key, it's not following the spec.(b74 was working ) containsKey(null) is returning false when some value is given for key'null'.
Given below is a testcase which explains this.
TestCase
---------
import java.util.jar.Attributes;
public class GetTests extends Attributes {
public static void main(String args[]) {
GetTests status= new GetTests();
status.getTest02();
}
/**
* tests get with valid arguments after storing a null value
*/
public void getTest02() {
try {
map.put((Attributes.Name)null,"value1");
} catch (NullPointerException e) {
System.out.println("Exception Thrown");
}
map.put(new Attributes.Name("key2"),"value2");
map.put(new Attributes.Name("key3"),"value3");
System.out.println("this.size() :"+this.size());
System.out.println("containsKey : "+containsKey(null) );
System.out.println("get((Attributes.Name)null) :"+map.get((Attributes.Name)null));
}
}
---------------------------------------------------------------
TestResult with b74 :
---------------------
javasoft16:/home/sa122568/Bugs/b75/GetTests 34 % java -version
java version "1.4.0-beta_refresh"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta_refresh-b74)
Java HotSpot(TM) Client VM (build 1.4.0-beta_refresh-b74, mixed mode)
javasoft16:/home/sa122568/Bugs/b75/GetTests 35 % javac *.java
javasoft16:/home/sa122568/Bugs/b75/GetTests 36 % java GetTests
this.size() :3
containsKey : true
get((Attributes.Name)null) :value1
-------------------------------------
TestResults with b75 :
---------------------
javasoft16:/home/sa122568/Bugs/b75/GetTests 41 % 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)
javasoft16:/home/sa122568/Bugs/b75/GetTests 42 % javac *.java
javasoft16:/home/sa122568/Bugs/b75/GetTests 43 % java GetTests
this.size() :3
containsKey : false
get((Attributes.Name)null) :null
------------------------------------------------------------------------
- duplicates
-
JDK-4491429 HashMap regression with key value null
- Closed