mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 23:04:50 +02:00
8238684: Override getOrDefault in immutable Map implementation
Reviewed-by: forax, psandoz, smarks
This commit is contained in:
parent
7552915d3f
commit
ac69c7894d
2 changed files with 24 additions and 0 deletions
|
@ -904,6 +904,20 @@ class ImmutableCollections {
|
|||
@Override public V replace(K key, V value) { throw uoe(); }
|
||||
@Override public boolean replace(K key, V oldValue, V newValue) { throw uoe(); }
|
||||
@Override public void replaceAll(BiFunction<? super K,? super V,? extends V> f) { throw uoe(); }
|
||||
|
||||
/**
|
||||
* @implNote {@code null} values are disallowed in these immutable maps,
|
||||
* so we can improve upon the default implementation since a
|
||||
* {@code null} return from {@code get(key)} always means the default
|
||||
* value should be returned.
|
||||
*/
|
||||
@Override
|
||||
public V getOrDefault(Object key, V defaultValue) {
|
||||
V v;
|
||||
return ((v = get(key)) != null)
|
||||
? v
|
||||
: defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
static final class Map1<K,V> extends AbstractImmutableMap<K,V> {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue