-
Bug
-
Resolution: Duplicate
-
P3
-
None
The default implementation of Map.replaceAll:
default void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
Objects.requireNonNull(function);
for (Map.Entry<K, V> entry : entrySet()) {
K k;
V v;
try {
k = entry.getKey();
v = entry.getValue();
} catch(IllegalStateException ise) {
throw new ConcurrentModificationException(ise);
}
entry.setValue(function.apply(k, v));
}
}
fails with UOE on ConcurrentSkipListMap because its entrySet returns immutable map entries. The default should instead do:
map.put(k, apply(k, v))
As part of a separate issue, HashMap, TreeMap, LinkedHashMap, ConcurrentHashMap, and ConcurrentSkipListMap should consider overridding Map.replaceAll, because the default is likely to be not as performant.
default void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
Objects.requireNonNull(function);
for (Map.Entry<K, V> entry : entrySet()) {
K k;
V v;
try {
k = entry.getKey();
v = entry.getValue();
} catch(IllegalStateException ise) {
throw new ConcurrentModificationException(ise);
}
entry.setValue(function.apply(k, v));
}
}
fails with UOE on ConcurrentSkipListMap because its entrySet returns immutable map entries. The default should instead do:
map.put(k, apply(k, v))
As part of a separate issue, HashMap, TreeMap, LinkedHashMap, ConcurrentHashMap, and ConcurrentSkipListMap should consider overridding Map.replaceAll, because the default is likely to be not as performant.
- relates to
-
JDK-8016432 Map implementations should override Map.replaceAll
-
- Closed
-