LoginSignup
0
0

More than 5 years have passed since last update.

【java】Mapのkeyの型に気をつけよう

Posted at

・整数リテラルはint型
・Mapは、keyの型が違うと、(一見キャストなりして推測できそうな値があったとしても、)存在しない扱いになる

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
    }
}
0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0