使用HashMap和迭代器时出现ConcurrentModificationException

时间:2016-02-25 04:02:58

标签: java hashmap concurrenthashmap

我有使用迭代器的方法。它有时会给我ConcurrentModificationException。任何人都可以建议我一个更好的解决方案。这是我的代码

float scale(float a, float b, float p) {
  return Math.min(a, b) + Math.abs(a - b) * p;
}

这就是我的堆叠竞赛的样子

private void addRequest(HttpUriRequest request) {
    Iterator<Entry<String, List<ComBase.OnReceiveListener<Entity>>>> iterator = ((Comm) _com)
        .getReceiveListeners().entryset().iterator();

    synchronized (iterator) {
        StringBuilder builder = new StringBuilder();
        boolean first = true;

        while (iterator.hasNext()) {
            Entry<String, List<ComBase.OnReceiveListener<Entity>>> entry = iterator.next();

            if (entry.getValue() != null && entry.getValue().size() > 0) {

                if (first) {
                    first = false;
                } else {
                    builder.append(", ");
                }
                builder.append(entry.getKey());
            }
        }
        request.setHeader(HttpHeaders.XAcceptEntities, builder.toString());
    }
}

0 个答案:

没有答案