java.util.ConcurrentModificationException
程序员文章站
2024-03-25 15:27:10
...
并发修改异常
java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextNode(HashMap.java:1437)
at java.util.HashMap$KeyIterator.next(HashMap.java:1461)
at org.hibernate.collection.internal.AbstractPersistentCollection$IteratorProxy.next(AbstractPersistentCollection.java:835)
at com.aui.stock.service.impl.CartServiceImpl.deleteCartItem(CartServiceImpl.java:147)
at com.aui.stock.service.impl.CartServiceImpl$$FastClassBySpringCGLIB$$323fef9e.invoke(<generated>)
当时用迭代器删除集合中的元素值需要注意并发修改异常
使用迭代器去删除 不要使用list集合去删除
错误的做法
Set<GoodsEntity> items = cart.getItems();
if(items!=null&&items.size()>0){
for(GoodsEntity goodsEntity : items){
if(snSet.size()>0){
if(snSet.contains(goodsEntity.getProduct().getSn())){
items.remove(goodsEntity);
}
}
}
}
正确的做法
Set<GoodsEntity> items = cart.getItems();
if(items!=null&&items.size()>0){
Iterator<GoodsEntity> iterator = items.iterator();
while(iterator.hasNext()){
GoodsEntity goodsEntity = iterator.next();
if(snSet.size()>0){
if(snSet.contains(goodsEntity.getProduct().getSn())){
iterator.remove();
}
}
}
}
上一篇: Python-for循环
推荐阅读
-
java.util.ConcurrentModificationException
-
java.util.ConcurrentModificationException
-
java.util.ConcurrentModificationException
-
出现java.util.ConcurrentModificationException 问题及解决办法
-
出现java.util.ConcurrentModificationException 问题及解决办法
-
Java集合:arraylist及java.util.ConcurrentModificationException
-
关于java List的remove方法导致的异常java.util.ConcurrentModificationException
-
java.util.ConcurrentModificationException详解
-
java.util.ConcurrentModificationException
-
Caused by: java.util.ConcurrentModificationException