1Optional<Employee> employee = employeeServive.getEmployee();
2// Sometimes an Employee has forgotten to write an up-to-date timesheet
3Optional<Timesheet> timesheet = employee.flatMap(Employee::askForCurrentTimesheet);
4// We don't want to do the heavyweight action of creating a new estimate if it will just be discarded
5client.bill(timesheet.orElseGet(EstimatedTimesheet::new));
1// Changed EmployeeServive to return an optional, no more nulls!
2Optional<Employee> employee = employeeServive.getEmployee();
3employee.ifPresent(e -> System.out.println(e.getId()));