public class OptionalTest {
public static void main(String[] args) {
Person person = new Person("zhangsan1", 18);
String name = getName(person);
System.out.println(name);
}
private static String getName(Person person) {
String name = Optional.ofNullable(person).map(x -> x.getName())
.orElse("unKnown");
return name;
}
}
代码抄来的,记录一下