參數來調用構造函數我有Employee類:在lambda表達式
public class Employee {
private Long id;
private String name;
private String externalId;
public Employee(Long id, String name, String externalId) {
this.id = id;
this.name = name;
this.externalId = externalId;
}
//getters, setter
}
和員工服務返回僱員(NULL是可能的)。
private void performEmployeeProcessing() {
Long employeeId = 2L;
Object o = Optional.ofNullable(employeeService.getById(employeeId))
.orElseGet(Employee::new, 1L, "", "");
System.out.println(o);
}
它說編譯錯誤
員工::新,1L, 「」, 「」
無法解析構造。
http://stackoverflow.com/questions/29835382/use-method-reference-with-parameter或http://stackoverflow.com/questions/31251629/java-8-supplier-with-arguments-in-the -constructor – Tom