天天看點

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

繼續閱讀