Don't remove elements from a map during iteration.

Doing so relies on implementation details which might change.

See #3911 / SI-8774.
Merge pull request #4018 from retronym/ticket/8774-concurrent-modification

Don't remove elements from a map during iteration.