Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Map<?, Optional<T>> to Map<?, T>
I have a Map
with Optional
values.
I want to filter it to remove empty values.
Map<K, Optional<T>> input;
Map<K, T> result = input.
// here is some code I'm looking for
.collect(Collector.toMap(???));
What is easiest way to achieve it?
All I can think of requires Optional.get
and I don't like that IDE warns me about it.
2 answers
The following users marked this post as Works for me:
User | Comment | Date |
---|---|---|
talex | (no comment) | Sep 11, 2024 at 18:30 |
Using Optional
as a value in a Map
will lead you to unnecessary complexity and confusion — as you can see.
The primary intention of Optional
is to serve as a return type, indicating that a value may or may not be present.
To achieve what you are looking for, you need to unwrap the values:
final var result = input.entrySet()
.stream()
.filter(it -> it.getValue().isPresent())
.collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().get()));
0 comment threads
Based on the code snippet in the question (which uses collect
etc), I'm assuming you want to use streams. I'm also infering that "empty values" means those values for which Optional.isEmpty()
returns false
(and consequently Optional.isPresent()
returns true
).
In that case, just create a stream for the map entries, filter the non-empty ones and collect to a new map, by getting the Optional
's values:
Map<K, T> result = input
// get stream of map entries
.entrySet().stream()
// check if value is present (which means it's not empty)
.filter(e -> e.getValue().isPresent()) // alternative: .filter(e -> ! e.getValue().isEmpty())
// collect to a new map, getting the values from the Optional
.collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().get()));
But of course you can also do it without streams, using the traditional loop approach:
Map<K, T> result = new HashMap<>();
for (Map.Entry<K, Optional<T>> entry : input.entrySet()) {
// value is present (AKA: not empty)
if (entry.getValue().isPresent()) { // alternative: if (! entry.getValue().isEmpty())
// add the Optional's value to result
result.put(entry.getKey(), entry.getValue().get());
}
}
IMO, the second approach makes it easier to change the Map
implementation - if you want a TreeMap
instead of a HashMap
, for example, just need to change the line Map<K, T> result = new HashMap<>();
to use whatever implementation type you want.
With streams, it's also possible, but not so straighforward. You'd have to change the collector to:
Map<K, T> result = input
....
.collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().get(), (a, b) -> a, TreeMap::new));
The fourth parameter tells what implementation type we'd like to return. But to use this, we must provide the third parameter, which is used to resolve collisions between values associated with the same key. In this case I'm assuming there will be no collisions (as the question has no indication that such situation can happen), so I'm just returning the first occurrence.
0 comment threads