You might want to start with Optional
instead and perform the Stream inside the Optional::map
method. If no result is present (str was null
), then return false
as requested:
Optional.ofNullable(str)
.map(string -> Stream.of("REG", "UNREG") // Your Stream as at your snippet
.anyMatch(string::equals)) // Returns if the str matches any
.orElse(false); // Returns false if str is null
Don't underestimate a simple:
str != null && Stream.of("REG", "UNREG").anyMatch(str::equals);
In case the first expression is false
(when str
is null
), the second part of the expression will not be evaluated thus never fails on NPE inside of Stream::anyMatch
.