Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在lambda表达式中设置一个变量_Java_Java 8 - Fatal编程技术网

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());
}