java map 的put、putIfAbsent、compute、computeIfAbsent、computeIfPresent的行为对比

1 篇文章 0 订阅
public static void main(String[] args) {
        Map<String,String> testMap = new HashMap<>();
        System.out.println("测试put方法返回值");
        System.out.println("first put return " + testMap.put("1", "a"));
        System.out.println("second put return " + testMap.put("1", "b"));
        System.out.println("结论:put后返回旧的value值");

        System.out.println("测试putIfAbsent行为");
        System.out.println("putIfAbsent return " + testMap.putIfAbsent("1", "c"));
        System.out.println("putIfAbsent放入重复值后 map=" + testMap);
        System.out.println("putIfAbsent return " + testMap.putIfAbsent("2", "a"));
        System.out.println("putIfAbsent放入非重复值后 map=" + testMap);
        testMap.remove("2");
        System.out.println("结论:putIfAbsent返回旧的value值,且当key存在时,不会放入新值,只有当key不存在时,才会放入新值,一般用于对新值的初始化");

        System.out.println("测试compute行为");

        System.out.println("compute return " + testMap.compute("1", (k, v) -> {
            //k,v is old key
            System.out.println("k=" + k);
            System.out.println("v=" + v);
            return "d";
        }));
        System.out.println("compute后的map=" + testMap);

        System.out.println("compute return " + testMap.compute("2", (k, v) -> {
            //k,v is old key
            System.out.println("k=" + k);
            System.out.println("v=" + v);
            return "d";
        }));
        System.out.println("compute后的map=" + testMap);
        testMap.remove("2");
        System.out.println("结论:返回新值,k为k(不是旧值的k,不存在也不会为空),v为旧值的value,返回新值的value,一般用于较复杂的新值计算");

        System.out.println("测试computeIfAbsent行为");
        System.out.println("computeIfAbsent return " + testMap.computeIfAbsent("1", (k) -> {
            //k,v is old key
            System.out.println("k=" + k);
            return "e";
        }));
        System.out.println("computeIfAbsent已存在key后的map=" + testMap);
        System.out.println("computeIfAbsent return " + testMap.computeIfAbsent("2", (k) -> {
            //k,v is old key
            System.out.println("k=" + k);
            return "e";
        }));
        System.out.println("computeIfAbsent不存在key后的map=" + testMap);
        testMap.remove("2");
        System.out.println("结论:computeIfAbsent的行为和compute一致,区别在于只有不存在key时,才会执行表达式");
        System.out.println("测试computeIfPresent行为");
        System.out.println("computeIfPresent return " + testMap.computeIfPresent("1", (k, v) -> {
            //有就执行
            System.out.println("k=" + k);
            System.out.println("v=" + v);
            return "f";
        }));
        System.out.println("computeIfPresent已存在key后的map=" + testMap);

        System.out.println("computeIfPresent return " + testMap.computeIfPresent("5", (k, v) -> {
            //有就执行
            System.out.println("k=" + k);
            System.out.println("v=" + v);
            return "f";
        }));
        System.out.println("computeIfPresent不存在key后的map=" + testMap);

        System.out.println("结论:computeIfAbsent的行为和compute一致,区别在于只有存在key时(和absent刚还相反),才会执行表达式");
    }

输出值:

测试put方法返回值
first put return null
second put return a
结论:put后返回旧的value值
测试putIfAbsent行为
putIfAbsent return b
putIfAbsent放入重复值后 map={1=b}
putIfAbsent return null
putIfAbsent放入非重复值后 map={1=b, 2=a}
结论:putIfAbsent返回旧的value值,且当key存在时,不会放入新值,只有当key不存在时,才会放入新值,一般用于对新值的初始化
测试compute行为
k=1
v=b
compute return d
compute后的map={1=d}
k=2
v=null
compute return d
compute后的map={1=d, 2=d}
结论:返回新值,k为k(不是旧值的k,不存在也不会为空),v为旧值的value,返回新值的value,一般用于较复杂的新值计算
测试computeIfAbsent行为
computeIfAbsent return d
computeIfAbsent已存在key后的map={1=d}
k=2
computeIfAbsent return e
computeIfAbsent不存在key后的map={1=d, 2=e}
结论:computeIfAbsent的行为和compute一致,区别在于只有不存在key时,才会执行表达式
测试computeIfPresent行为
k=1
v=d
computeIfPresent return f
computeIfPresent已存在key后的map={1=f}
computeIfPresent return null
computeIfPresent不存在key后的map={1=f}
结论:computeIfAbsent的行为和compute一致,区别在于只有存在key时(和absent刚还相反),才会执行表达式

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值