我非常想使用Map.computeIfAbsent,但是因为lambdas在本科课程中已经太长了.
几乎直接来自文档:它给出了一个旧方法的例子:
Map<String, Boolean> whoLetDogsOut = new ConcurrentHashMap<>();
String key = "snoop";
if (whoLetDogsOut.get(key) == null) {
Boolean isLetOut = tryToLetOut(key);
if (isLetOut != null)
map.putIfAbsent(key, isLetOut);
}
Run Code Online (Sandbox Code Playgroud)
而新的方式:
map.computeIfAbsent(key, k -> new Value(f(k)));
Run Code Online (Sandbox Code Playgroud)
但在他们的例子中,我认为我并没有"得到它".我如何转换代码以使用新的lambda表达方式?