ConcurrentModificationException 在向映射中添加项目时发生。

huangapple go评论79阅读模式
英文:

ConcurentModificationException while adding items in a map

问题

我在向与特定键对应的现有列表添加项目时遇到了java.util.ConcurrentModificationException异常。

以下是我的代码:

final Map<String, List<Apple>> feedToApplesMap = new HashMap<>();
for (final Apple apple : appleList) {
    final List<String> feedDocumentIds = apple.getFeedDocumentIds();
    for (final String feedId : feedDocumentIds) {
        final List<Apple> appleListForFeed = feedToApplesMap
                .getOrDefault(feedId, new ArrayList<>());
        appleListForFeed.add(apple);
        feedToApplesMap.put(feedId, appleListForFeed);
    }
}

我无法使用迭代器来解决这个问题?有什么建议吗?

英文:

I am facing java.util.ConcurrentModificationException while adding items to an existing list corresponding to a key

Here is my code

final Map&lt;String, List&lt;Apple&gt;&gt; feedToApplesMap = new HashMap&lt;&gt;();
for (final Apple Apple : AppleList) {
    final List&lt;String&gt; feedDocumentIds = Apple.getFeedDocumentIds();
    for (final String feedId : feedDocumentIds) {
        final List&lt;Apple&gt; AppleListForFeed = feedToApplesMap
                .getOrDefault(feedId, new ArrayList&lt;&gt;());
        AppleList.add(Apple);
        feedToApplesMap.put(feedId, AppleListForFeed);
    }
}

I am unable to use iterator to solve this ? Any suggestions ?

答案1

得分: 1

似乎你把苹果存放在错误的地方。应该是 AppleListForFeed.add(Apple) 而不是 AppleList.add(Apple)

英文:

It seems like you're storing apples in a wrong place. Shouldn't it be

AppleListForFeed.add(Apple) instead of AppleList.add(Apple) ?

答案2

得分: 1

我同意 @andbi 的观点,问题是由于一个拼写错误引起的。但是只是为了异常情况,通过这样做 for (final Apple Apple : AppleList) 你仍然无意中使用了 Iterator,这就是为什么这里 AppleList.add(Apple); 会抛出 ConcurrentModificationException 异常,你可以像这样做来避免使用 Iterator:

final Map<String, List<Apple>> feedToApplesMap = new HashMap<>();
int size = AppleList.size();
for (int i = 0; i < size; i++) {
    final Apple Apple = AppList.get(i);
    final List<String> feedDocumentIds = Apple.getFeedDocumentIds();
    for (final String feedId : feedDocumentIds) {
        final List<Apple> AppleListForFeed = feedToApplesMap
                .getOrDefault(feedId, new ArrayList<>());
        AppleList.add(Apple);
        feedToApplesMap.put(feedId, AppleListForFeed);
    }
}

请注意,增强型 for 循环只是带有 Iterator 的普通 for 循环的一种语法糖,下面这两种写法是等价的:

for (Iterator<T> i = c.iterator(); i.hasNext(); )

for (T t : c)

这是文档链接:The For-Each Loop

英文:

I agree with @andbi that the question is due to a typo. But just for the exception, By doing so for (final Apple Apple : AppleList) you are still using Iterator unintentionally, that's why here AppleList.add(Apple); will throw ConcurrentModificationException, you can do it like this to avoid using Iterator:

final Map&lt;String, List&lt;Apple&gt;&gt; feedToApplesMap = new HashMap&lt;&gt;();
int size = AppleList.size();
for (int i = 0; i &lt; size; i++) {
    final Apple Apple = AppList.get(i);
    final List&lt;String&gt; feedDocumentIds = Apple.getFeedDocumentIds();
    for (final String feedId : feedDocumentIds) {
        final List&lt;Apple&gt; AppleListForFeed = feedToApplesMap
                .getOrDefault(feedId, new ArrayList&lt;&gt;());
        AppleList.add(Apple);
        feedToApplesMap.put(feedId, AppleListForFeed);
    }
}

Please be aware that enhanced for-loop is just a syntatic sugar for for-loop with Iterator, the following two are the same:

    for (Iterator&lt;T&gt; i = c.iterator(); i.hasNext(); )
 
    for (T t : c)
    

Here is the document: The For-Each Loop

答案3

得分: 0

这种情况发生在我们使用for循环或for each遍历列表,并且在遍历过程中添加了一些元素,因此请使用迭代器来遍历列表:

val iterator = yourList.iterator()
while (iterator.hasNext()) {
    val currentItem = iterator.next()
}

现在在遍历过程中添加或删除任何其他元素。

英文:

This happens when we are iterating a list using for loop or for each and some elements are added in between during iterating so use iterator or iterating a list:

val iterator = yourList.iterator()
    while (iterator.hasNext()) {
        val currentItem = iterator.next()
    }

now add or delete any other element in your list during iterating.

huangapple
  • 本文由 发表于 2020年10月16日 13:41:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/64383479.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定