The change of value when inserting duplicate keys in HashMap

public static void main(String[] args) {
    
    
        HashMap<Integer, String> map = new HashMap<>();
        //向map中插入数据
        map.put(1, "sss");
        map.put(2, "aaa");
        map.put(3, "ccc");
        //修改key=1的value为ddd
        String a = map.put(1, "ddd");
        System.out.println(map);
        System.out.println(a);
    }

Insert picture description here
It can be seen that the value value of key=1 in the map has become ddd, and the old value value sss is returned

Guess you like

Origin blog.csdn.net/Hambur_/article/details/110084032