リストをループしながら、条件に応じてリストのアイテムを削除したいと思います。以下のコードをご覧ください。
これでは、ConcurrentModification
という例外が発生してしまいます。
for (Object a : list) {
if (a.getXXX().equalsIgnoreCase("AAA")) {
logger.info("this is AAA........should be removed from the list ");
list.remove(a);
}
}
どのようにすればよいのでしょうか?
//first find out the removed ones
List removedList = new ArrayList();
for(Object a: list){
if(a.getXXX().equalsIgnoreCase("AAA")){
logger.info("this is AAA........should be removed from the list ");
removedList.add(a);
}
}
list.removeAll(removedList);