Java 8 Stream String Null Or Empty Filter
In java 11 there is a new method Predicate::not.
So you can filter out empty string :
list.stream()
.filter(Objects::nonNull)
.filter(Predicate.not(String::isEmpty))
You can create your own Strings
class with your own predicate:
public class Strings {
public static boolean isNotNullOrEmpty (String str) {
return str != null && !str.isEmpty();
}
}
Then in your code:
.filter(Strings::isNotNullOrEmpty)
But as @fge mentionned, you can't use that on a Map.Entry<?,?>
...
If you prefer to use commons-lang3, StringUtils has
isEmpty()
isNotEmpty()
isBlank()
isNotBlank()
These methods can be used in filters as method references:
this.stringList.stream().filter(StringUtils::isNotBlank);
or as lambdas:
this.stringList.stream().filter(s -> StringUtils.isNotBlank(s));
You can write your own predicate:
final Predicate<Map.Entry<?, String>> valueNotNullOrEmpty
= e -> e.getValue() != null && !e.getValue().isEmpty();
Then just use valueNotNullOrEmpty
as your filter argument.