I need to use a getter which is 3 levels down in the object.
response.getServiceError().getErrorCode()
It is possible one or more objects could be NULL. Now, I am doing this
if (response != null && response.getServiceError() != null && response.getServiceError().getErrorCode() != null && response.getServiceError().getErrorCode().equals("errCode123")) {
     //doSomething;
}
Is there a better and/or elegant way to construct that if condition?
Use Optional!
Optional.ofNullable(response)
    .map(Response::getServiceError)
    .map(ServiceError::getErrorCode)
    .filter(code -> code.equals("errCode123"))
    .ifPresent(code -> {
        // doSomething
    });
                        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