if (user != null) { Address address = user.getAddress(); if (address != null) { Country country = address.getCountry(); if (country != null) { String isocode = country.getIsocode(); if (isocode != null) { isocode = isocode.toUpperCase(); }else{ isocode = "2"; } } } }
简化:
isocode = Optional.ofNullable(user).map(Address::getAddress).map(Country::getCountry).map(String::getIsocode).orElse("2");