Maps keySet is a collection that supports remove() but is otherwise read-only

One explanation may be that the underlying Java HashMap code behaves that way; this HashMap source code includes this code:

884     public Set<K> More ...keySet() {
885         Set<K> ks = keySet;
886         return (ks != null ? ks : (keySet = new KeySet()));
887     }
888
889     private final class KeySet extends AbstractSet<K> {
890         public Iterator<K> iterator() {
891             return newKeyIterator();
892         }
893         public int size() {
894             return size;
895         }
896         public boolean contains(Object o) {
897             return containsKey(o);
898         }
899         public boolean remove(Object o) {
900             return HashMap.this.removeEntryForKey(o) != null;
901         }
902         public void More clear() {
903             HashMap.this.clear();
904         }
905     }

and this documentation for the keySet method:

Returns a Set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

Seems like the designer felt that as removal via the set could be implemented it should be implemented: given that collections are mutable in general probably a reasonable decision.


It's not a read-only map, per se (the error is shared with other truly read-only collections, like Trigger.new). It allows you to remove values, which also removes them from the map. You can use the removeAll, retainAll, remove, and clear methods to remove some or all of the mapped value pairs. I actually wish they allowed add and addAll methods, because it would be an efficient way to prepopulate a map with null values.

As a useful example, if you wanted to retain only values that match in two different maps, you can:

map1.keySet().retainAll(map2.keySet());

This is clearly much more efficient than what you'd be able to do without this capability. While it's rare that you'll ever have a need for this, there are legitimate cases for being able to remove keys and their values by manipulating the Set directly.

And yes, if you intend to just get a copy of the keys that you can use later, you should definitely clone the Set before using it.

Tags:

Set

Map

Apex