I am newbie here and trying my way hard through java 8 streams + lambda. Can someone help me with the below piece of code? Need to find the age of first person from the list which is optional. This list could be null.
Missing from the code - null check for the first element of the list. Would like to have this check as well.
Something equivalent to below piece of code.
//input parameter
//Optional<List<Person>> persons
public Integer getAgeOfFirstPerson(Optional<List<Person>> persons) {
if (!persons.isPresent()) {
return null;
}
Integer age = persons.get()
.get(0)
.getAge();
return age;
}
//Person.java
class Person {
Integer age;
//getters and setters
}
The findFirst() method returns an Optional describing the first element of the given stream if Stream is non-empty, or an empty Optional if the stream is empty.
The findFirst() method finds the first element in a Stream. So, we use this method when we specifically want the first element from a sequence. When there is no encounter order, it returns any element from the Stream. According to the java.
Assuming the List
inside your Optional
can be empty, then you should account for this case as well. The easiest would thus be:
return persons.flatMap(list -> list.stream().findFirst()).map(Person::getAge).orElse(null);
This unfortunately does not handle a null
first list entry as findFirst()
throws a NullPointerException
when the first element of a Stream
is null
(as Holger noted in the comments).
So if you want to skip null
list entries and take the first non-null one, do as follows:
return persons.flatMap(list -> list.stream().filter(Objects::nonNull).findFirst())
.map(Person::getAge)
.orElse(null);
Otherwise, if instead you always want the first entry even when it is null
, you could adapt it like this:
return persons.flatMap(list -> list.stream().limit(1).filter(Objects::nonNull).findFirst())
.map(Person::getAge)
.orElse(null);
however this really becomes quite convoluted, so it is simpler to just get back to testing whether the list is empty:
return persons.filter(list -> !list.isEmpty())
.map(list -> list.get(0))
.map(Person::getAge)
.orElse(null);
You could use Optional.map
public int getAgeOfFirstPerson(Optional<List<Person>> persons){
return persons.map(people -> people.get(0).getAge()).orElse(0);
}
Do note, primitives do not have null
(they are not objects), hence have replaced that with 0
.
Or further improved as answered by @Alexey, if need be to check for Person being null as well.
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