HashMap series: HashMap's key and value null value problems

  • Can the key value of HashMap be null?
  • Can the value of HashMap be null?
  • Can the key and value of HashMap be null at the same time?
    • Both the key and value of HashMap can be null.

Look at the following demo:

 public static void main(String[] args) {

        HashMap<String, String> map = new HashMap<>();

        map.put(null, null);
        System.out.println("null->" + map.get(null));

        map.put(null, "xxx");
        System.out.println("null->" +map.get(null));

        map.put("1", null);
        System.out.println("1->" +map.get("1"));

        map.put("2", null);
        System.out.println("2->" +map.get("2"));
    }

//输出结果
null->null
null->xxx
1->null
2->null
  • A new question is here. How to calculate the hashCode when the key value is null?

    • Time is limited, to be continued.

Guess you like

Origin blog.csdn.net/zhangjin1120/article/details/114916529