英文:
ConcurrentModificationException when I do a addAll for a Set
问题
我在下面的代码中遇到了ConcurrentModificationException异常,发生在allStates.addAll(states)
这一行。我该如何避免这个问题?
public synchronized Set<String> getAllStates(String clientName, Map<String, Set<String>> allClientStates) {
Set<String> allStates = new ConcurrentSkipListSet<>();
final Set<String> keySet = allClientStates.keySet();
for(String key: keySet) {
Set<String> states = allClientStates.get(key);
if(states != null)
allStates.addAll(states);
}
return allStates;
}
这是堆栈跟踪的顶部部分:
exception: null
java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextNode(HashMap.java:1445)
at java.util.HashMap$KeyIterator.next(HashMap.java:1469)
at java.util.AbstractCollection.addAll(AbstractCollection.java:343)
at com.xxx.config.ClientDashboardConfig.getAllStates(ClientDashboardConfig.java:312)
英文:
I get a concurrentModificationException at allStates.addAll(states)
in the code below. How can I avoid this?
public synchronized Set<String> getAllStates(String clientName, Map<String, Set<String>> allClientStates) {
Set<String> allStates = new ConcurrentSkipListSet<>();
final Set<String> keySet = allClientStates.keySet();
for(String key: keySet) {
Set<String> states = allClientStates.get(key);
if(states != null)
allStates.addAll(states);
}
return allStates;
}
This is the top of the stacktrace
exception": "null\njava.util.ConcurrentModificationException\n\tat java.util.HashMap$HashIterator.nextNode(HashMap.java:1445)\n\tat java.util.HashMap$KeyIterator.next(HashMap.java:1469)\n\tat java.util.AbstractCollection.addAll(AbstractCollection.java:343)\n\tat com.xxx.config.ClientDashboardConfig.getAllStates(ClientDashboardConfig.java:312)
答案1
得分: 2
allClientStates.values().stream()
.flatMap(keys -> keys.stream())
.collect(Collectors.toSet());
英文:
allClientStates.values().stream()
.flatMap(keys -> keys.stream())
.collect(Collectors.toSet());
答案2
得分: 0
我不明白为什么需要ConcurrentSkipListSet和clientName。这段代码是否完整?
另外,不清楚是否有JVM版本限制。
使用Java 8,你可以这样做:
allClientStates.values()
.stream()
.flatMap(Collection::stream)
.collect(Collectors.toSet());
英文:
I didn't get why do you need the ConcurrentSkipListSet and the clientName. Is this code complete?
Also not clear if there's JVM version limitation
Using java 8 you can do like this:
allClientStates.values()
.stream()
.flatMap(Collection::stream)
.collect(Collectors.toSet());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论