'Efficiently iterate over a map for a matching key
I want to iterate over a java Map and get a case insensitive key match and get a value out.
m -> m.entrySet().stream().filter(e -> e.getKey().equalsIgnoreCase(target).getValue()
Is there an efficient way to do the above?
Solution 1:[1]
No, there is not. If you do not use the equals and hashCode keying of a map, there is no more efficient way to search a map in the general case than linearly.
If you could guarantee ahead of time that the map only had lowercase keys, and you looked up target.toLowerCase(), that would allow you to use the map's efficient get function, but it's not clear if that's possible.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Louis Wasserman |
