-
Bug
-
Resolution: Not an Issue
-
P4
-
None
-
22
-
generic
-
generic
ADDITIONAL SYSTEM INFORMATION :
$ uname -a
Linux name 6.5.0-27-generic #28~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Fri Mar 15 10:51:06 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
$ java -version
openjdk version "22" 2024-03-19
OpenJDK Runtime Environment (build 22+36-2370)
OpenJDK 64-Bit Server VM (build 22+36-2370, mixed mode, sharing)
A DESCRIPTION OF THE PROBLEM :
public class BugTreeMap {
public static void main(String[] args) {
java.util.Map<Object,Object> map = new java.util.TreeMap<>();
map.put("a", "b");
System.err.println("map.get(\"a\")="+map.get("a"));
map.put(1, "a");
System.err.println("map.get(1)="+map.get(1));
}
}
/*
When using a TreeMap with different types of keys, the code compiles but fails to run. The first error is gotten by running the program above. The second error is gotten by switching the map.put order. Expectation is to allow different types of keys and be able to get their respective values since the Map key and value types are both Object.
$ java BugTreeMap
map.get("a")=b
Exception in thread "main" java.lang.ClassCastException: class java.lang.String cannot be cast to class java.lang.Integer (java.lang.String and java.lang.Integer are in module java.base of loader 'bootstrap')
at java.base/java.lang.Integer.compareTo(Integer.java:74)
at java.base/java.util.TreeMap.put(TreeMap.java:849)
at java.base/java.util.TreeMap.put(TreeMap.java:569)
at BugTreeMap.main(BugTreeMap.java:8)
$ java BugTreeMap
map.get(1)=a
Exception in thread "main" java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.String (java.lang.Integer and java.lang.String are in module java.base of loader 'bootstrap')
at java.base/java.lang.String.compareTo(String.java:144)
at java.base/java.util.TreeMap.put(TreeMap.java:849)
at java.base/java.util.TreeMap.put(TreeMap.java:569)
at BugTreeMap.main(BugTreeMap.java:8)
*/
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
In a shell terminal:
Save code as BugTreeMap.java
Compile: javac BugTreeMap.java
Run: java BugTreeMap
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Expected result should be the output of the println statements.
ACTUAL -
Actual result is the errors shown in the description.
---------- BEGIN SOURCE ----------
See the description.
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Workaround is using a HashMap instead.
FREQUENCY : always
$ uname -a
Linux name 6.5.0-27-generic #28~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Fri Mar 15 10:51:06 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
$ java -version
openjdk version "22" 2024-03-19
OpenJDK Runtime Environment (build 22+36-2370)
OpenJDK 64-Bit Server VM (build 22+36-2370, mixed mode, sharing)
A DESCRIPTION OF THE PROBLEM :
public class BugTreeMap {
public static void main(String[] args) {
java.util.Map<Object,Object> map = new java.util.TreeMap<>();
map.put("a", "b");
System.err.println("map.get(\"a\")="+map.get("a"));
map.put(1, "a");
System.err.println("map.get(1)="+map.get(1));
}
}
/*
When using a TreeMap with different types of keys, the code compiles but fails to run. The first error is gotten by running the program above. The second error is gotten by switching the map.put order. Expectation is to allow different types of keys and be able to get their respective values since the Map key and value types are both Object.
$ java BugTreeMap
map.get("a")=b
Exception in thread "main" java.lang.ClassCastException: class java.lang.String cannot be cast to class java.lang.Integer (java.lang.String and java.lang.Integer are in module java.base of loader 'bootstrap')
at java.base/java.lang.Integer.compareTo(Integer.java:74)
at java.base/java.util.TreeMap.put(TreeMap.java:849)
at java.base/java.util.TreeMap.put(TreeMap.java:569)
at BugTreeMap.main(BugTreeMap.java:8)
$ java BugTreeMap
map.get(1)=a
Exception in thread "main" java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.String (java.lang.Integer and java.lang.String are in module java.base of loader 'bootstrap')
at java.base/java.lang.String.compareTo(String.java:144)
at java.base/java.util.TreeMap.put(TreeMap.java:849)
at java.base/java.util.TreeMap.put(TreeMap.java:569)
at BugTreeMap.main(BugTreeMap.java:8)
*/
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
In a shell terminal:
Save code as BugTreeMap.java
Compile: javac BugTreeMap.java
Run: java BugTreeMap
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Expected result should be the output of the println statements.
ACTUAL -
Actual result is the errors shown in the description.
---------- BEGIN SOURCE ----------
See the description.
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Workaround is using a HashMap instead.
FREQUENCY : always