I'm trying to figure out how I can rewrite this to use streams and filters to narrow my criteria down and remove from map if necessary.
Iterator<Map.Entry<String,Object>> iter = listOfPossibleParams.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<String,Object> entry = iter.next();
if(entry.getValue() instanceof String) {
if (StringUtils.isBlank((String) entry.getValue())) {
iter.remove();
}
}
}
I was initially thinking something like this, but it obiviously doesnt work as syntax errors :
listOfPossibleParams.entrySet()
.stream()
.filter(p -> p.getValue() instanceof String)
.removeIf(e -> StringUtils.isBlank((String)e.getValue()));
If you can modify the Map in place, then you could do:
listOfPossibleParams.values()
.removeIf(v -> v instanceof String && StringUtils.isBlank((String) v));
If you want to build a new Map instead, you could have:
listOfPossibleParams.entrySet()
.stream()
.filter(p -> {
Object v = p.getValue();
return !(v instanceof String) || !StringUtils.isBlank((String) v);
})
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
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