> If I understand correctly, this class represents an immutable empty map. As
> a result, operations like put or remove have been implemented to throw
> UnsupportedOperationException; this makes sense to me. However, this is
> also the implementation for computeIfPresent, which I believe may be too
> disruptive: the definition of this method says “If the value for the
> specified key is present and non-null, attempts to compute a new mapping
> given the key and its current mapped value.“; for an empty map, this could
> be a safe no-op, instead of an exception.

The spec for Map.computeIfPresent states that it should throw 
UnsupportedOperationException if Map.put is not supported, which is the case 
for this map.
The exception is listed as "(optional)" though, for which the docs specify "may 
throw an exception or it may succeed, at the option of the implementation." so 
it's not entirely clear to me if it would be OK for computeIfPresent to not 
throw.

That being said -- for me it makes more sense that Collections.emptyMap() 
follows the same logic as Map.of(), or Collections.unmodifiableMap​(new 
HashMap<>()) -- and the latter two both throw from computeIfPresent.

/Michael

Reply via email to