As already discussed, the API designers do not assume that the developer wants to treat null
values and absent values the same way.
If you still want to do that, you may do it explicitly by applying the sequence
.map(Optional::ofNullable).findFirst().flatMap(Function.identity())
to the stream. The result will be an empty optional in both cases, if there is no first element or if the first element is null
. So in your case, you may use
String firstString = strings.stream()
.map(Optional::ofNullable).findFirst().flatMap(Function.identity())
.orElse(null);
to get a null
value if the first element is either absent or null
.
If you want to distinguish between these cases, you may simply omit the flatMap
step:
Optional<String> firstString = strings.stream()
.map(Optional::ofNullable).findFirst().orElse(null);
System.out.println(firstString==null? "no such element":
firstString.orElse("first element is null"));
This is not much different to your updated question. You just have to replace "no such element"
with "StringWhenListIsEmpty"
and "first element is null"
with null
. But if you don’t like conditionals, you can achieve it also like:
String firstString = strings.stream()
.map(Optional::ofNullable).findFirst()
.orElseGet(()->Optional.of("StringWhenListIsEmpty"))
.orElse(null);
Now, firstString
will be null
if an element exists but is null
and it will be "StringWhenListIsEmpty"
when no element exists.
String
here, what if it's a list that represents a column in the DB? The value of the first row for that column can benull
. – Multiplicitynull
is a perfectly acceptable value in Java, generally speaking. In particular, it is a valid element for anArrayList<String>
. Like any other value, however, there are limitations on what can be done with it. "Don't ever usenull
" is not useful advice, as you cannot avoid it. – PlatefindFirst()
, there's nothing else you want to do. – Multiplicity