Reputation: 5237
I have a Map<String, ClassA> results
.
When I do this, I get a ConcurrentModificationException:
results.entrySet().stream().map((entry) -> {
ClassA objA = entry.getValue();
if(objA.getList() != null) {
objA.getList().forEach(x -> {
if(x.getAttr() != null && x.getAttr.containsKey(key)) {
List<String> y = x.getAttr().get(key);
y.replaceAll(ClassB::transformationFunc);
}
});
}
})
Basically what I am trying to do is if my results, has a value of ClassA, check for each element of the list if there is any Attribute with a given key. If so, replace the values of the key with a given transformation function.
public static String transformationFunc(String input) {
try {
JSONObject inputJson = (JSONObject) jsonParser.parse(input);
return String.format("%s_%s", inputJson.get(key1), inputJson.get(key2));
} catch (ParseException e) {
//log
//throw
}
}
Upvotes: 0
Views: 108
Reputation: 3491
Basically, you get a ConcurrentModificationException
whenever you modify a collection that you are iterating over.
The only way in which you are allowed to modify a collection that you are iterating over is by explicitly using an Iterator
. If you want to do work on a collection while you're iterating over it, you have to do it on a working copy of the collection instead.
Upvotes: 4