[Solved] Map.containsKey() vs Map.keySet().stream().anyMatch() [closed]


Map is an interface, it does not make sense to speak about efficiency or performance without a specific implementation.

But let’s take HashMap as one of the common implementations.

HashMap.containsKey is amortized O(1).

Map.keySet().stream().anyMatch(predicate) is O(N) as you iterate over keys. And we don’t even mention all the objects created by this statement.

5

solved Map.containsKey() vs Map.keySet().stream().anyMatch() [closed]