我正在重构Java 8的代码,我想用Optional替换null检查.
public Employee findEmployeeById(String id) {
List<Employee> empList = .. //some db query
return (empList.isEmpty() ? null : empList.get(0));
}
Run Code Online (Sandbox Code Playgroud)
Optional.ofNullable(empList.get(0)) 什么时候不会起作用 IndexOutofBoundException
或者我应该理想地用null替换null Optional.empty()?