1/ Working code:
public Student process (int id, name){
Optional<Student> studentOpt = myrepo.findById(id);
studentOpt.isPresent() {
return updateStudent(id, name);
} else {
return createStudent(id, name);
}
2/ I try to change it to 'full lambdas code' (not working):
public Student process (int id, name){
Optional<Student> studentOpt = myrepo.findById(id);
return studentOpt.ifPresentOrElse(student-> return updateStudent(id, name), () -> return createStudent(id, name));
}
1/ should I change it to full lambda? what is the cleanest?
2/ if yes, how ?
return
calls from inside the call toifPresentOrElse
, and it should work. You might have to makeid
andname
effectively final.