I want to remove all entries where value is an empty Optional from the map. It would seem that nothing complicated, but I am trying to find a better solution that I have.
Input:
I have the following Map:
Map<String, Function<String, Optional<String>>> attributesToCalculate = new HashMap<>();
Where key - just a String and value - reference to method which returns Optional < String >
Output:
As a result, I want to get
Map<String, String> calculatedAttributes
(excluding entries where value was an empty Optional)
Here is my solution
return attributesToCalculate.entrySet()
.stream()
.map(entry -> Pair.of(entry.getKey(), entry.getValue().apply(someString)))
.filter(entry -> entry.getValue().isPresent())
.collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().get()));
But I don't like the .filter part because then I have to invoke .get() on Optional in collect part.
Is there а better way (maybe without .get invocation) to solve this problem? Thanks.
HashMap. clear() method in Java is used to clear and remove all of the elements or mappings from a specified HashMap. Parameters: The method does not accept any parameters. Return Value: The method does not return any value.
The empty method of the Optional method is used to get the empty instance of the Optional class. The returned object doesn't have any value.
The class invariant is that an Optional can never hold a null value, so either the map should fail or it should return an empty Optional .
Not a very pretty one, and similar to for-loop:
return attributesToCalculate.entrySet().stream().collect(HashMap::new, (sink, entry) -> {
entry.getValue().apply(someString).ifPresent(v -> sink.put(entry.getKey(), v));
}, Map::putAll);
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