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)