为了避免ConcurrentModificationException ,我正在诉诸以下内容:
List<String> tobeRemoved = new ArrayList<String>();
for (Object propertyKey : suppliedContent.keySet()) {
key = (String) propertyKey;
if (EqualityUtils.isMatching(rgx, key)) {
tobeRemoved.add(key);
}
}
for (String o : tobeRemoved) {
suppliedContent.remove(o);
}
有没有更简洁的方法?
最佳答案
我认为使用 iterator
并在匹配时调用 iterator
上的 remove()
会做同样的事情。
Iterator<String> supplieIter = suppliedContent.keySet().iterator();
while(supplieIter.hasNext()){
key = supplieIter.next();
if (EqualityUtils.isMatching(rgx, key)) {
supplieIter.remove()
}
}
编辑:手写代码。可能存在语法错误。
关于java - 从属性中删除元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12283267/