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刚还相反),才会执行表达式