Skip to content

Commit

Permalink
some default method overrides in TreeMap
Browse files Browse the repository at this point in the history
  • Loading branch information
liach committed Oct 20, 2023
1 parent 8f5f440 commit c80b770
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/java.base/share/classes/java/util/TreeMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,12 @@ public V get(Object key) {
return (p==null ? null : p.value);
}

@Override
public V getOrDefault(Object key, V defaultValue) {
Entry<K,V> p = getEntry(key);
return (p==null ? defaultValue : p.value);
}

public Comparator<? super K> comparator() {
return comparator;
}
Expand Down Expand Up @@ -922,6 +928,17 @@ public V remove(Object key) {
return oldValue;
}

@Override
public boolean remove(Object key, Object value) {
Entry<K,V> p = getEntry(key);
if (p != null && Objects.equals(value, p.value)) {
deleteEntry(p);
return true;
}

return false;
}

/**
* Removes all of the mappings from this map.
* The map will be empty after this call returns.
Expand Down Expand Up @@ -1873,10 +1890,26 @@ public final V get(Object key) {
return !inRange(key) ? null : m.get(key);
}

public final V remove(Object key) {
public V getOrDefault(Object key, V defaultValue) {
return !inRange(key) ? defaultValue : m.getOrDefault(key, defaultValue);
}

public V remove(Object key) {
return !inRange(key) ? null : m.remove(key);
}

public boolean remove(Object key, Object value) {
return inRange(key) && m.remove(key, value);
}

public boolean replace(K key, V oldValue, V newValue) {
return inRange(key) && m.replace(key, oldValue, newValue);
}

public V replace(K key, V value) {
return !inRange(key) ? null : m.replace(key, value);
}

public final Map.Entry<K,V> ceilingEntry(K key) {
return exportEntry(subCeiling(key));
}
Expand Down

0 comments on commit c80b770

Please sign in to comment.