0

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);
                }
            }
        }
    }
Turing85
  • 18,217
  • 7
  • 33
  • 58
cNgamba
  • 111
  • 1
  • 10
  • I can't figure out why the hashset is not updated while i am iterating the keys of the ConcurrentHashMap - i tried the iterator method - it dint work ! – cNgamba Aug 16 '20 at 07:55
  • Thanks @Turing85 - went to the the link provided and one of the method helped - by gathering a list of values that i want to remove. – cNgamba Aug 17 '20 at 14:52

0 Answers0