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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章