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 same way you should avoid `null` `List` variables: is there really a difference between an empty `Optional
– Didier L Aug 22 '18 at 23:00>` and a non-empty one containing an empty `List`? If not, don't use `Optional` at all, use the empty `List` instead. If yes, maybe you should define your own type to clearly describe it.
>` already describes it perfectly clearly. OTOH, I was never convinced by the arguments referenced in https://stackoverflow.com/a/24564612/9204 :)
– Alexey Romanov Aug 23 '18 at 06:35>`” describe? Is there a semantic difference between an absent list and an empty list?
– Holger Aug 23 '18 at 07:58