station
station

Reputation: 7145

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?

Upvotes: 0

Views: 167

Answers (1)

Louis Wasserman
Louis Wasserman

Reputation: 198033

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.

Upvotes: 2

Related Questions