英文:
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<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<>());
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<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);
}
}
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<T> 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论