Java 在lambda表达式中设置一个变量
我想更新一个条目,如下所示:Java 在lambda表达式中设置一个变量,java,java-8,Java,Java 8,我想更新一个条目,如下所示: public Entry updateEmail(String nom, EntryRequest entryReq) { Optional<Entry> optional = entryRepository.findByNom(nom); Entry updatedEntry = null; optional.ifPresent(entry -> { if(!String
public Entry updateEmail(String nom, EntryRequest entryReq) {
Optional<Entry> optional = entryRepository.findByNom(nom);
Entry updatedEntry = null;
optional.ifPresent(entry -> {
if(!StringUtils.isEmpty(entryReq.getEmail())){
entry.setEmail(entryReq.getEmail());
}
updatedEntry = save(entry);
});
optional.orElseThrow(() -> new NotFoundException(this.getClass().getName()));
return updatedEntry;
}
public Entry updateEmail(字符串nom,EntryRequest entryReq){
可选=entryRepository.findByNom(nom);
条目updateEntry=null;
可选。如果存在(条目->{
如果(!StringUtils.isEmpty(entryReq.getEmail())){
entry.setEmail(entryReq.getEmail());
}
update尝试=保存(条目);
});
可选.orelsetrow(()->newNotFoundException(this.getClass().getName());
返回更新尝试;
}
此代码向我提供以下错误消息:
lambda表达式中使用的变量应为final或effective
决赛
我如何解决这个问题?这里不要使用lambda
Optional<Entry> optional = entryRepository.findByNom(nom);
Entry updatedEntry = null;
if(optional.isPresent()){
Entry entry=optional.get();
if(!StringUtils.isEmpty(entryReq.getEmail())){
entry.setEmail(entryReq.getEmail());
}
updatedEntry = save(entry);
});
optional.orElseThrow(() -> new NotFoundException(this.getClass().getName()));
return updatedEntry;
Optional=entryRepository.findByNom(nom);
条目updateEntry=null;
if(可选的.isPresent()){
Entry=optional.get();
如果(!StringUtils.isEmpty(entryReq.getEmail())){
entry.setEmail(entryReq.getEmail());
}
update尝试=保存(条目);
});
可选.orelsetrow(()->newNotFoundException(this.getClass().getName());
返回更新尝试;
甚至更好
Optional<Entry> optional = entryRepository.findByNom(nom);
Entry entry=optional.orElseThrow(() -> new NotFoundException(this.getClass().getName()));
if(!StringUtils.isEmpty(entryReq.getEmail())){
entry.setEmail(entryReq.getEmail());
}
return save(entry);
Optional=entryRepository.findByNom(nom);
Entry=optional.orelsetrow(()->newNotFoundException(this.getClass().getName());
如果(!StringUtils.isEmpty(entryReq.getEmail())){
entry.setEmail(entryReq.getEmail());
}
返回保存(条目);
这里不要使用lambda
Optional<Entry> optional = entryRepository.findByNom(nom);
Entry updatedEntry = null;
if(optional.isPresent()){
Entry entry=optional.get();
if(!StringUtils.isEmpty(entryReq.getEmail())){
entry.setEmail(entryReq.getEmail());
}
updatedEntry = save(entry);
});
optional.orElseThrow(() -> new NotFoundException(this.getClass().getName()));
return updatedEntry;
Optional=entryRepository.findByNom(nom);
条目updateEntry=null;
if(可选的.isPresent()){
Entry=optional.get();
如果(!StringUtils.isEmpty(entryReq.getEmail())){
entry.setEmail(entryReq.getEmail());
}
update尝试=保存(条目);
});
可选.orelsetrow(()->newNotFoundException(this.getClass().getName());
返回更新尝试;
甚至更好
Optional<Entry> optional = entryRepository.findByNom(nom);
Entry entry=optional.orElseThrow(() -> new NotFoundException(this.getClass().getName()));
if(!StringUtils.isEmpty(entryReq.getEmail())){
entry.setEmail(entryReq.getEmail());
}
return save(entry);
Optional=entryRepository.findByNom(nom);
Entry=optional.orelsetrow(()->newNotFoundException(this.getClass().getName());
如果(!StringUtils.isEmpty(entryReq.getEmail())){
entry.setEmail(entryReq.getEmail());
}
返回保存(条目);
如果初始条目存在,您实际上可以使用Optional的map方法来处理保存:
public Entry updateEmail(String nom, EntryRequest entryReq) {
Optional<Entry> optional = entryRepository.findByNom(nom);
Entry updatedEntry = optional.map(entry -> {
if(!StringUtils.isEmpty(entryReq.getEmail())){
entry.setEmail(entryReq.getEmail());
}
return save(entry);
}).orElseThrow(() -> new NotFoundException(this.getClass().getName()));
return updatedEntry;
}
public Entry updateEmail(字符串nom,EntryRequest entryReq){
可选=entryRepository.findByNom(nom);
Entry UpdateEntry=可选。映射(条目->{
如果(!StringUtils.isEmpty(entryReq.getEmail())){
entry.setEmail(entryReq.getEmail());
}
返回保存(条目);
}).OrelsThrow(()->new NotFoundException(this.getClass().getName());
返回更新尝试;
}
简而言之:
public Entry updateEmail(String nom, EntryRequest entryReq) {
Optional<Entry> optional = entryRepository.findByNom(nom);
return optional.map(entry -> {
if(!StringUtils.isEmpty(entryReq.getEmail())){
entry.setEmail(entryReq.getEmail());
}
return save(entry);
}).orElseThrow(() -> new NotFoundException(this.getClass().getName()));
}
public Entry updateEmail(字符串nom,EntryRequest entryReq){
可选=entryRepository.findByNom(nom);
返回可选的.map(条目->{
如果(!StringUtils.isEmpty(entryReq.getEmail())){
entry.setEmail(entryReq.getEmail());
}
返回保存(条目);
}).OrelsThrow(()->new NotFoundException(this.getClass().getName());
}
如果初始条目存在,您实际上可以使用Optional的map方法来处理保存:
public Entry updateEmail(String nom, EntryRequest entryReq) {
Optional<Entry> optional = entryRepository.findByNom(nom);
Entry updatedEntry = optional.map(entry -> {
if(!StringUtils.isEmpty(entryReq.getEmail())){
entry.setEmail(entryReq.getEmail());
}
return save(entry);
}).orElseThrow(() -> new NotFoundException(this.getClass().getName()));
return updatedEntry;
}
public Entry updateEmail(字符串nom,EntryRequest entryReq){
可选=entryRepository.findByNom(nom);
Entry UpdateEntry=可选。映射(条目->{
如果(!StringUtils.isEmpty(entryReq.getEmail())){
entry.setEmail(entryReq.getEmail());
}
返回保存(条目);
}).OrelsThrow(()->new NotFoundException(this.getClass().getName());
返回更新尝试;
}
简而言之:
public Entry updateEmail(String nom, EntryRequest entryReq) {
Optional<Entry> optional = entryRepository.findByNom(nom);
return optional.map(entry -> {
if(!StringUtils.isEmpty(entryReq.getEmail())){
entry.setEmail(entryReq.getEmail());
}
return save(entry);
}).orElseThrow(() -> new NotFoundException(this.getClass().getName()));
}
public Entry updateEmail(字符串nom,EntryRequest entryReq){
可选=entryRepository.findByNom(nom);
返回可选的.map(条目->{
如果(!StringUtils.isEmpty(entryReq.getEmail())){
entry.setEmail(entryReq.getEmail());
}
返回保存(条目);
}).OrelsThrow(()->new NotFoundException(this.getClass().getName());
}