天天看点

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

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 的值

继续阅读