Optional
Optional类型(P11)
如何使用Optional值(P11-12)
//没有任何匹配时,使用默认值""
String result = optionalString.orElse("");
//The wrapped string , or "" if none.
//调用代码来计算默认值
String result = optionalString.orElseGet(
() -> Local.getDefault().getDisplayName();
//The function is only called when needed
//没有值时抛出异常
String result = optionaleString.orElseThrow(IllegalStateException::new);
//Supply a method that yields an exception object创建Optional值(P13-15)
Last updated