Home > database >  Iterating through a HashMap while removing AND updating the collection
Iterating through a HashMap while removing AND updating the collection

Time:06-19

I have a count map where I keep track of the numbers of characters from a string. I want to iterate over that map, decrement the currently visited character count AND remove it if it reaches zero.

How can that be done in Java?

HashMap<Character, Integer> characterCount = new HashMap<>();
characterCount.put('a', 2);
characterCount.put('b', 1);
characterCount.put('c', 1);

Iterator<Map.Entry<Character, Integer>> iterator = characterCount.entrySet().iterator();
while (iterator.hasNext()) {
    Map.Entry<Character, Integer> entry = iterator.next();

    // Decrement the chosen character from the map
    if (entry.getValue() == 1) {
        iterator.remove();
    } else {
        characterCount.put(entry.getKey(), entry.getValue() - 1);
    }

    // Call some logic the relies on the map with the remaining character count.
    // I want the characterCount.size() to return zero when there is no character with count > 0
    doSomeLogic(characterCount);

    // Restore the character to the map
    characterCount.put(entry.getKey(), entry.getValue());
}

The above code results in a ConcurrentModificationException.

CodePudding user response:

Since Map#entrySet returns a view of the mappings in the map, directly set the value of the Entry to update it.

if (entry.getValue() == 1) {
    iterator.remove();
} else {
    entry.setValue(entry.getValue() - 1);
}

CodePudding user response:

Here is one way. Map.computeIfPresent will remove the entry when it becomes null. Using the ternary operator either decrements the value or replaces with null if it is presently 1 (about to be decremented).

Map<Character, Integer> map = new HashMap<>();
map.put('a', 2);
map.put('b', 1);
map.put('c', 1);

map.computeIfPresent('c', (k,v)-> v == 1 ? null : v-1);
map.computeIfPresent('a', (k,v)-> v == 1 ? null : v-1);

System.out.println(map);

prints

{a=1, b=1}

So, here is how it would work for you, replacing your iterator and while loop.

for (char ch : characterCount.keySet()) {
      characterCount.computeIfPresent(ch, (k,v)-> v == 1 ? null : v-1);
}
  • Related