Library: an efficient way to raise an exception if an item already exists in Map when adding an item #6118
Labels
area-library
core-n
P2
A bug or feature request we're likely to work on
type-enhancement
A request for a change that isn't a bug
Scenario: I want to make sure Keys are not duplicated in a map.
Current implementation:
if(map.contains(key)) {
throw 'dupe key';
}
map[key] = value;
I'd rather not dig into the map more than once.
Suggestion: add an optional paramater to putIfAbsent
V putIfAbsent(K key, V ifAbsent(), [void ifPresent(V value)]);
One could then throw in the ifPresent callback.
I could imagine other uses for the ifPresent param as well. It also has nice symmetry with ifAbsent().
The text was updated successfully, but these errors were encountered: