map 的computeIfAbsent,computeIfPresent,compute几个方法的区别和使用

本文介绍了Map接口中computeIfAbsent,computeIfPresent和compute三个方法的区别和使用场景。computeIfAbsent仅在键对应的值不存在时执行函数并插入结果;computeIfPresent则在值存在时执行函数并更新;而compute不论值是否存在都会执行函数,用返回值更新键值对。" 123930066,8250319,Java连接Redis测试:利用Jedis验证服务状态,"['Redis', 'Java开发', '缓存系统', '数据库连接', 'Jedis']

map是我们经常用到的类,但computeIfAbsent,computeIfPresent,compute由于之前没有使用过这里写几个列子来。

1、computeIfAbsent

   public static void main(String[] args) {
        Map<String, String> name = new HashMap<>();
        name.put("key1", "value1");
        //computeIfAbsent  计算如果缺失,只有当value 不存在时候,才会运行function里面的方法
        //设置value值为 return 的值
        String value1 = name.computeIfAbsent("key1", (key) -> {
                    System.out.println("computeIfAbsent中的key1:"+key);
                    return "123";
                }
        );
        System.out.println(value1);
        System.out.println(name.get("key1"));
        System.out.println("=====================");
        String  value3 = name.computeIfAbsent("key3", (key) -> {
                    System.out.println("computeIfAbsent中的key3:"+key);
                    return "123";
                }
        );
        System.out.println(value3);
        System.out.println(name.get("key3"));
    }

运行结果:

value1
value1
=====================
computeIfAbsent中的key3:key3
123
123

可以看到map.computeIfAbsent 就是 只有当value不存在存在时候,才会运行function方法,return值为put的值。

2、computeIfPresent

public static void main(String[] args) {
        Map<String, String> name = new HashMap<>();
        name.put("key1", "value1");
        //computeIfPresent  计算如果缺失,只有当key 存在时候,才会运行function里面的方法
        //设置value值为 return 的值

        String value1 = name.computeIfPresent("key1", (key,value) -> {
                    System.out.println("computeIfPresent 中的key1:"+key);
                    System.out.println("computeIfPresent 中的value1:"+value);
                    return "123";
                }
        );
        System.out.println(value1);
        System.out.println(name.get("key1"));
        System.out.println("------------------------");
        String value3 = name.computeIfPresent("key3", (key,value) -> {
                    System.out.println("computeIfPresent 中的key3:"+key);
                    System.out.println("computeIfPresent 中的value3:"+value);
                    return "123";
                }
        );
        System.out.println(value3);
        System.out.println(name.get("key3"));
    }

运行结果

computeIfPresent 中的key1:key1
computeIfPresent 中的value1:value1
123
123
------------------------
null
null

可以看到map.computeIfPresent 就是 只有当value存在时候,才会运行function方法,return值为put的值。

3、compute

   public static void main(String[] args) {
        Map<String, String> name = new HashMap<>();
        name.put("key1", "value1");
        //compute  无论value是否有值,才会运行function里面的方法
        //设置value值为 return 的值

            String value1 = name.compute("key1", (key, value) -> {
            System.out.println("compute 中的key1:"+key);
            System.out.println("compute 中的value1:"+value);
            return "123";
        });

        System.out.println(value1);
        System.out.println(name.get("key1"));
        System.out.println("------------------------");
        String value3= name.compute("key3", (key, value) -> {
            System.out.println("compute 中的key3:"+key);
            System.out.println("compute 中的value3:"+value);
            return "123";
        });
        System.out.println(value3);
        System.out.println(name.get("key3"));
    }

运行结果:

compute 中的key1:key1
compute 中的value1:value1
123
123
------------------------
compute 中的key3:key3
compute 中的value3:null
123
123

可以看到compute 无论value是否有值,才会运行function里面的方法,设置value值为 return 的值

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值