问题描述
我需要使用在对象内3级以下的吸气剂。
response.getServiceError().getErrorCode()
if (response != null && response.getServiceError() != null && response.getServiceError().getErrorCode() != null && response.getServiceError().getErrorCode().equals("errCode123")) {
//doSomething;
}
是否存在一种更好和/或更优雅的方式来构造条件?
解决方法
使用Optional
!
Optional.ofNullable(response)
.map(Response::getServiceError)
.map(ServiceError::getErrorCode)
.filter(code -> code.equals("errCode123"))
.ifPresent(code -> {
// doSomething
});