【转载】JDK8中computeIfAbsent方法
转载
麻烦的方法
Map<String, List<String>> map = new HashMap<>();
List<String> list;
// 一般这样写
list = map.get("list-1");
if (list == null) {
list = new LinkedList<>();
map.put("list-1", list);
}
list.add("one");
// 使用 computeIfAbsent 可以这样写
list = map.computeIfAbsent("list-1", k -> new ArrayList<>());
list.add("one");
方便的方法
validMap.computeIfAbsent(id, key -> new ArrayList<>()).add(model);
博 主 :夏秋初
地 址 :https://www.cnblogs.com/xiaqiuchu/p/16626953.html
如果对你有帮助,可以点一下 推荐 或者 关注 吗?会让我的分享变得更有动力~
转载时请带上原文链接,谢谢。
地 址 :https://www.cnblogs.com/xiaqiuchu/p/16626953.html
如果对你有帮助,可以点一下 推荐 或者 关注 吗?会让我的分享变得更有动力~
转载时请带上原文链接,谢谢。