I want to filter several objects from a map as follows:
Currently I do it using two methods
Map<String, MyObject > map = scenarioFieldsMap.entrySet().stream()
.filter(e -> e.getKey().contains("["))
.collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue()));
scenarioFieldsMap.entrySet().removeIf(e -> e.getKey().contains("["));
Is there a better way to filter and remove?
The second step can be more efficient if instead of iterating over all the keys (or entries) you only remove the keys present in the other map :
scenarioFieldsMap.keySet().removeAll(map.keySet());
I'm assuming you meant to remove the entries from the original scenarioFieldsMap
and not from the new map that you create in the first step.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With