java map 的put、putIfAbsent、compute、computeIfAbsent、computeIfPresent的行爲對比

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剛還相反),纔會執行表達式

 

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