Java 8 - Optional<CustomizedObject> get first element of the list within CustomizedObject

meso :

I am not sure how to do this

class Department {
    String deptName;
    List<Person> employees;
}

class Person {
    String personName;
}

The problem statement is to fetch the first name of the person working in a particular department. This department can be optional. So this is how my method looks -

String getFirstPerson(Optional<Department> department, String defaultName) {
// TODO: 
}

I know the traditional way of doing this but would like to see some Java 8 + lambda way to simplify this. Still a newbie here - so please pardon if I am not using the correct format.

I also have a default name to use in case we dont find that value.

P.S. I know it is not best practice to send Optional as method parameter. This is not the actual code. I am just trying to simplify it.

Deadpool :

You can use the map function on Optional to get the employees list and then use stream get the first name or return defaultName. Even incase if Optional is empty you will get the defaultName

String getFirstPerson(Optional<Department> department, String defaultName) {

   return department.map(d->d.getEmployees().stream().map(Person::getPersonName).findFirst().orElse(defaultName)).orElse(defaultName));
}

If you have a chance of getting null on getEmployees you can use below approach

department.map(Department::getEmployees)
          .filter(Objects::nonNull)
          .map(emp->emp.stream().map(Person::getPersonName).findFirst().orElse(defaultName)).orElse(defaultName)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=305882&siteId=1