Java 中Map的Put() 与putIfAbsent() 方法区别

版权声明:学习交流为主,未经博主同意禁止转载,禁止用于商用。 https://blog.csdn.net/u012965373/article/details/82657280
    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        map.putIfAbsent("A", "1");
        // get 出来的值会被覆盖
        map.put("A", "3");
        // get 出来的值不会被覆盖,如果之前的值是空值,则会被覆盖
        map.putIfAbsent("A", "2");
        System.out.println(map.get("A"));
    }

猜你喜欢

转载自blog.csdn.net/u012965373/article/details/82657280