测试Map各种设置方法

compute

compute方法可以根据指定key值,找到map中对应的键值对,进行一番计算后,返回新value。
如果新value不为null,且key值在map中本就存在,则新value替换对应旧value;
如果key值在map中原本就不存在,则新的映射建立。
如果新value为null,将删除旧的映射。

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

        // compute方法可以根据指定key值,找到map中对应的键值对,进行一番计算后,返回新value。
        // 如果新value不为null,且key值在map中本就存在,则新value替换对应旧value;
        // 如果key值在map中原本就不存在,则新的映射建立。
        // 如果新value为null,将删除旧的映射。
        System.out.println("测试compute");
        System.out.println(map.compute("compute", (key, oldValue) -> {
            return "compute1";
        }));
        System.out.println(map.compute("compute", (key, oldValue) -> {
            return "compute2";
        }));
        System.out.println(map.compute("compute", (key, oldValue) -> {
            return null;
        }));

输出结果

测试compute
compute1
compute2
null

computeIfAbsent

如果指定key不存在或关联值为null,就把函数接口返回的值作为指定key的value(除非返回值为null)。存在则不做任何操作,返回存在的值

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

//如果指定key不存在或关联值为null,就把函数接口返回的值作为指定key的value(除非返回值为null)。存在则不做任何操作,返回存在的值
        System.out.println("测试computeIfAbsent");
        System.out.println(map.computeIfAbsent("computeIfAbsent", (key) -> {
            return null;
        }));
        System.out.println(map.computeIfAbsent("computeIfAbsent", (key) -> {
            return "computeIfAbsent2";
        }));
        System.out.println(map.computeIfAbsent("computeIfAbsent", (key) -> {
            return "computeIfAbsent3";
        }));
        System.out.println(map.computeIfAbsent("computeIfAbsent", (key) -> {
            return null;
        }));

输出结果

测试computeIfAbsent
null
computeIfAbsent2
computeIfAbsent2
computeIfAbsent2

computeIfPresent

如果指定key对应的value存在且不是null,就将函数接口的返回值和key建立联系。

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

       // 如果指定key对应的value存在且不是null,就将函数接口的返回值和key建立联系。
        System.out.println("测试computeIfPresent");
        System.out.println(map.computeIfPresent("computeIfPresent", (key,value) -> {
            return "computeIfPresent1";
        }));
        System.out.println(map.computeIfPresent("computeIfPresent", (key, oldValue) -> {
            return "computeIfPresent2";
        }));

输出结果

测试computeIfPresent
null
null

putIfAbsent

为空则put,返回null。不为空则不put并返回对于的value。运行put null

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

        // 为空则put,返回null。不为空则不put并返回对于的value。运行put null
        System.out.println("测试putIfAbsent");
        System.out.println(map.putIfAbsent("putIfAbsent", "putIfAbsent1"));
        System.out.println(map.putIfAbsent("putIfAbsent", "putIfAbsent2"));
        System.out.println(map.putIfAbsent("putIfAbsent", "putIfAbsent3"));

输出结果

测试putIfAbsent
null
putIfAbsent1
putIfAbsent1

put

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

        System.out.println("测试put");
        System.out.println(map.put("put", "put1"));
        System.out.println(map.put("put", "put2"));
        System.out.println(map.put("put", "put3"));

输出结果

测试put
null
put1
put2