1

为了避免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);
        }

有没有更清洁的方法?

4

2 回答 2

4

我认为在匹配时使用和调用会做同样的事情iteratorremove()iterator

Iterator<String> supplieIter  = suppliedContent.keySet().iterator();

    while(supplieIter.hasNext()){
        key = supplieIter.next();

        if (EqualityUtils.isMatching(rgx, key)) {
           supplieIter.remove()
        }
    }

编辑:手写代码。可能存在语法错误。

于 2012-09-05T14:04:26.127 回答
2

您可以使用 Iterator.remove() 之类的

Properties suppliedContent = ...
for (Iterator iter = suppliedContent.keySet().iterator(); iter.hasNext(); ) 
    if (EqualityUtils.isMatching(rgx, (String) iter.next())) 
        iter.remove();
于 2012-09-05T14:07:46.563 回答