简体   繁体   English

迭代map时出现ConcurrentModificationException

[英]ConcurrentModificationException when iterating over map

I am getting ConcurrentModificationException when iterating over a content of a map 迭代地图内容时,我收到ConcurrentModificationException

    for (String sourceKey : sMap.getContent().keySet()) {

        List<String> sourceValues = sMap.getValues(sourceKey);
        List<String> targetValues = tMap.getValues(sourceKey);

        if (areMultiple(sourceValues)) {
        ....
    }

sMap.getContent() returns Map<String, List<String>> , of which i am getting keys only. sMap.getContent()返回Map<String, List<String>> ,我只获取密钥。

Note that no modification of map's content is done. 请注意,不会修改地图的内容。 I am only iterating here. 我只是在这里迭代。

What am i doing wrong here? 我在这做错了什么? How can i correct the situation when exception is thrown? 如何在抛出异常时纠正这种情况?

This will occur if the underlying collection that is being iterated over is modified by anything other than the Iterator itself. 如果正在迭代的基础集合被Iterator本身以外的任何东西修改,则会发生这种情况。

Try using a ConcurrentHashMap instead of a plain HashMap 尝试使用ConcurrentHashMap而不是普通的HashMap

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM