[Java] Be careful of the key type of Map

-Integer literals are int type -Map will be treated as nonexistent if the key type is different (even if there is a value that seems to be guessed by the cast)

import java.util.*;

public class MapTest {
    public static void main(String[] argv) {
        Map<Short, Object> a = new HashMap<>();
        a.put(new Short("1"), new Object());
        System.out.println(a);  // {1=java.lang.Object@7852e922}
        System.out.println(a.get(1));   // null !!
        System.out.println(a.get(new Short("1")));  // java.lang.Object@7852e922
    }
}

Recommended Posts

[Java] Be careful of the key type of Map
I tried the input / output type of Java Lambda ~ Map edition ~
[Java10] Be careful of using var and generics together
[Java] To know the type information of type parameters at runtime
[Rails] Where to be careful in the description of validation
Be sure to compare the result of Java compareTo with 0
[Java] Delete the elements of List
[Java version] The story of serialization
[Java] Correct comparison of String type
The origin of Java lambda expressions
How to increment the value of Map in one line in Java
Get the result of POST in Java
[Java] Implicit type cast (AOJ10-sum of numbers)
Check the contents of the Java certificate store
Duplicate Map sorted by key in Java
Examine the memory usage of Java elements
[Java] Get the day of the specific day of the week
Reverse Key from Value in Java Map
[Java] Introducing the Generics Boundary Wildcard Type
Compare the elements of an array (Java)
JAVA (Map)
[day: 5] I summarized the basics of Java
What are the updated features of java 13
Easily measure the size of Java Objects
Reason to add L to the number to be put in Java long type
Looking back on the basics of Java
[Basic knowledge of Java] About type conversion
Output of the book "Introduction to Java"
[Java] Comparison of String type character strings
Java Summary of frequently searched type conversions
The story of writing Java in Emacs
[Java] Check the number of occurrences of characters
[Java] [Spring] Test the behavior of the logger
Make the variable that stores the ID an ID type instead of an int / long (Java)
Summarize the life cycle of Java objects to be aware of in Android development
Be careful about upgrade if you use | etc. in the URL of Tomcat
About the meaning of type variables, E, T, etc. used in generics used in Java
Be absolutely careful when putting the result of and / or in a variable!
Be careful with requests and responses when using the Serverless Framework in Java
The story of low-level string comparison in Java
[Java] Handling of JavaBeans in the method chain
JAVA: jar, aar, view the contents of the file
[Android] [Java] Manage the state of CheckBox of ListView
Things to be aware of when writing Java
About the description order of Java system properties
About the idea of anonymous classes in Java
Be careful if you find SHIFT-JIS in Java
The order of Java method modifiers is fixed
The intersection type introduced in Java 10 is amazing (?)
[Java] Access the signed URL of s3 (signed version 2)
The story of learning Java in the first programming
Measure the size of a folder in Java
[Java] Get the length of the surrogate pair string
[Java] The confusing part of String and StringBuilder
[Note] Java: Measures the speed of string concatenation
I compared the characteristics of Java and .NET
Feel the passage of time even in Java
Calculate the similarity score of strings with JAVA
[Java] Be aware of short circuits (short-circuit evaluation)
Try the free version of Progate [Java II]
Organized memo in the head (Java --Data type)