'C# Dictionary: Java HashMap.compute() equivalent in C# Dictionary?
Java's HashMap.compute() method is very powerful, in that it can:
- add a
(key, value)pair, ifkeywas not previously present - update
valuethrough a lambda (via its return), ifkeywas previously present - remove
keyfrom the hashmap, if the supplied lambda returnsnull- user doesn't need to doHashMap.remove(key)(which is one extra hashmap lookup)
C++'s unordered_map relies on the iterator returned from the find method, and uses erase(iterator) to avoid extra lookup
Rust's HashMap provides the Entry API, which uses the Entry::remove method to delete the entry without extra lookup
However, I don't seem to find similar facility in C#'s Dictionary API - could anyone shed some light on the design of C#'s Dictionary API?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
