The documentation for ConcurrentModificationException
explains this :
If a single thread issues a sequence of method invocations that violates the contract of an object, the object may throw this exception. For example, if a thread modifies a collection directly while it is iterating over the collection with a fail-fast iterator, the iterator will throw this exception.
Treat your problem by starting from someMap
and you won't have to modify concurrently, hence the exception thrown.
A solution could thus be :
someMap.entrySet()
.stream()
.filter(entry -> jsonObject.has(entry.getKey()))
.forEach(entry -> {
jsonObject.put(entry.getKey(), entry.getValue());
jsonObject.remove(entry.getKey());
});
I don't exactly get why you'd put()
and then remove()
but then again this is not essential to the solution.
I assume you're using JSON-lib by the methods you're using, so you could also collect to a map :
someMap.entrySet()
.stream()
.filter(entry -> jsonObject.has(entry.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
JSONObject has a putAll(Map map) to make quick work of this.
It would be then a matter of benchmarking for whichever method suits you.