I am starting to learn java and going through the following issue
While trying to update values(Hashset) of ConcurrentHashMap when iterating over it - ConcurrentModificationError is shown while debugging - although the code didn't crash
This is the scenario:
concurrentMap =
Key -> A
Value -> ["a","b","c","d"]
Key -> B
value -> ["a", "j", "k", "l", "m"]
when i try to remove "a" while iterating over A it fails to update the hashset properly and fails to loop I saw the hashset update as follows
[a, b, c, d]
[b,c]
How can I remove 'a' from key A while i am iterating over A and update the hashSet properly ?
private ConcurrentMap<String, HashSet<String>> concurrentMap = new ConcurrentHashMap<>();
private void parseItems(Item testItem) throws Exception {
Map<String, HashSet<String>> itemMap = ConcurrentHashMap <>(Myclass.getIdMap(testItem));
concurrentMap = new ConcurrentHashMap<>(itemMap);
for (String type : concurrentMap.keySet()) {
for (String id : concurrentMap.get(type)) {
if (type.equals("MyType")) {
myList = function1(MyType, id);
} else {
myList = function2(defaultType, id, "ID");
}
removeParsedIdFromCurrentMap(type, id);
for (Item item : myList) {
ConcurrentMap<String, HashSet<String>> newItemConcurrentMap = new ConcurrentHashMap<>(Myclass.getIdMap(item));
addToConcurentMap(newItemConcurrentMap);
}
}
}
}
private void removeParsedIdFromCurrentMap(String type, String id) {
HashSet<String> currentIdSet = concurrentMap.get(type);
//This removes the value 'a'
if (currentIdSet != null) {
Iterator<String> iter = currentIdSet.iterator();
while (iter.hasNext()) {
String toRemoveId = iter.next();
if (toRemoveId.contains(id)) {
iter.remove();
}
}
//How can I update the values of key A while iterating A?
updateConcurrentMap();
}
}
private void addToConcurentMap(ConcurrentMap<IOMType, HashSet<String>> newItemConcurrentMap ) {
for (String type : newItemConcurrentMap .keySet()) {
for (String id : newItemConcurrentMap .get(type)) {
if (!checkIdParsed(type, id)) {
concurrentMap.putIfAbsent(type, new HashSet<>());
concurrentMap.get(type).add(id);
}
}
}
}