If you have a default value for your Desktop, you could try with Optional.orElse:
Desktop defaultDesktop = ...;
Desktop desktop = Optional.ofNullable(status)
.map(Status::getDesktop)
.orElse(defaultDesktop);
However, you don't have to necessarily work inside a lambda expression with Optional.ifPresent. You could perfectly use a method that receives a Desktop instance, which would act as the Consumer argument of Optional.ifPresent:
Desktop desktop = Optional.ofNullable(status)
.map(Status::getDesktop)
.ifPresent(this::workWithDesktop);
Then:
void workWithDesktop(Desktop desktop) {
// do whatever you need to do with your desktop
}
If you need additional arguments (apart from the desktop itself), you could use a lambda expression that invokes the method instead:
String arg1 = "hello";
int arg2 = 10;
Desktop desktop = Optional.ofNullable(status)
.map(Status::getDesktop)
.ifPresent(desktop -> this.workWithDesktop(desktop, arg1, arg2));
And then:
void workWithDesktop(Desktop desktop, String arg1, int arg2) {
// do whatever you need to do with your desktop, arg1 and arg2
}
Answer from fps on Stack OverflowVideos
If you have a default value for your Desktop, you could try with Optional.orElse:
Desktop defaultDesktop = ...;
Desktop desktop = Optional.ofNullable(status)
.map(Status::getDesktop)
.orElse(defaultDesktop);
However, you don't have to necessarily work inside a lambda expression with Optional.ifPresent. You could perfectly use a method that receives a Desktop instance, which would act as the Consumer argument of Optional.ifPresent:
Desktop desktop = Optional.ofNullable(status)
.map(Status::getDesktop)
.ifPresent(this::workWithDesktop);
Then:
void workWithDesktop(Desktop desktop) {
// do whatever you need to do with your desktop
}
If you need additional arguments (apart from the desktop itself), you could use a lambda expression that invokes the method instead:
String arg1 = "hello";
int arg2 = 10;
Desktop desktop = Optional.ofNullable(status)
.map(Status::getDesktop)
.ifPresent(desktop -> this.workWithDesktop(desktop, arg1, arg2));
And then:
void workWithDesktop(Desktop desktop, String arg1, int arg2) {
// do whatever you need to do with your desktop, arg1 and arg2
}
I was told here just last week that it's a code smell to even use Optional like this at all, so
Desktop desktop = (status != null)? status.getDesktop() : null;
Style 2 isn't going Java 8 enough to see the full benefit. You don't want the if ... use at all. See Oracle's examples. Taking their advice, we get:
Style 3
// Changed EmployeeServive to return an optional, no more nulls!
Optional<Employee> employee = employeeServive.getEmployee();
employee.ifPresent(e -> System.out.println(e.getId()));
Or a more lengthy snippet
Optional<Employee> employee = employeeServive.getEmployee();
// Sometimes an Employee has forgotten to write an up-to-date timesheet
Optional<Timesheet> timesheet = employee.flatMap(Employee::askForCurrentTimesheet);
// We don't want to do the heavyweight action of creating a new estimate if it will just be discarded
client.bill(timesheet.orElseGet(EstimatedTimesheet::new));
If you're using Optional as a "compatibility" layer between an older API that may still return null, it may be helpful to create the (non-empty) Optional at the latest stage that you're sure that you have something. E.g., where you wrote:
Optional<Employee> employeeOptional = Optional.ofNullable(employeeService.getEmployee()); if(employeeOptional.isPresent()){ Employee employeeOptional= employeeOptional.get(); System.out.println(employee.getId()); }
I'd opt toward:
Optional.of(employeeService) // definitely have the service
.map(EmployeeService::getEmployee) // getEmployee() might return null
.map(Employee::getId) // get ID from employee if there is one
.ifPresent(System.out::println); // and if there is an ID, print it
The point is that you know that there's a non-null employee service, so you can wrap that up in an Optional with Optional.of(). Then, when you call getEmployee() on that, you may or may not get an employee. That employee may (or, possibly, may not) have an ID. Then, if you ended up with an ID, you want to print it.
There's no need to explicitly check for any null, presence, etc., in this code.