在看代码时发现
To safely remove from a collection while iterating over it you should use an Iterator.
For example:
for(Long id : ids){
if(id.equals(userId))
ids.remove(id);
}To safely remove from a collection while iterating over it you should use an Iterator.
For example:
List<String> names = ....
Iterator<String> i = names.iterator();
while (i.hasNext()) {
// Do something
i.remove();
}
本文介绍了一种在遍历集合过程中安全地删除元素的方法。直接在for-each循环中使用remove()方法可能导致并发修改异常。正确的做法是使用Iterator迭代器进行遍历并删除。
2459

被折叠的 条评论
为什么被折叠?



