新版本的jdk 8的Concurrent Hash Map有两个新方法.
computeIfAbsent
computeIfPresent
putIfAbsent - 旧方法
我理解putIfAbsent和computeIfAbsent的用例.但我不确定何时使用computeIfPresent.另外,如果我现在有computeIfPresent,为什么还需要putIfAbsent. putIfAbsent会创建至少一个额外的值实例.
原因只是具有向后兼容性吗?
例如我的列表包含{4,6,6,7,7,8},我想要最终结果= {6,6,7,7}
一种方法是遍历列表并消除唯一值(在这种情况下为4,8).
有没有其他有效的方式而不是循环列表?我问过这个问题,因为我工作的清单非常大?我的代码是
List<Long> duplicate = new ArrayList();
for (int i = 0; i < list.size(); i++) {
Long item = (Long) list.get(i);
if (!duplicate.contains(item)) {
duplicate.add(item);
}
}
Run Code Online (Sandbox Code Playgroud)