Java 8可选多级空检查

Java 8可选多级空检查,java,java-8,null,optional,Java,Java 8,Null,Optional,我正在编写一个程序,该程序使用返回Optional的方法,我需要迭代该方法并创建一个新对象。我该怎么做 import java.util.Optional; class Info { String name; String profileId; Info(String name, String profileId) { this.name = name; this.profileId = profileId; } } clas

我正在编写一个程序,该程序使用返回
Optional
的方法,我需要迭代该方法并创建一个新对象。我该怎么做

import java.util.Optional;

class Info {
    String name;
    String profileId;

    Info(String name, String profileId) {
        this.name = name;
        this.profileId = profileId;
    }
}

class Profile {
    String profileId;
    String profileName;

    Profile(String profileId, String profileName) {
        this.profileId = profileId;
        this.profileName = profileName;
    }
}

class Content {
    String infoName;
    String profileName;

    Content(String infoName, String profileName) {
        this.infoName = infoName;
        this.profileName = profileName;
    }

    public java.lang.String toString() {
        return "Content{" + "infoName='" + infoName + '\'' + ", profileName='" + profileName + '\'' + '}';
    }
}

class InfoService {
    Optional<Info> findByName(String name){ //todo implementation }
}

class ProfileService {
   Optional<Profile> findById(String id) { //todo implementation }
}

class ContentService {

    Content createContent(Info i, Profile p) {
        return new Content(i.name, p.profileName);
    }

    Content createContent(Info i) {
        return new Content(i.name, null);
    }
}

public static void main(String[] args) {

    InfoService infoService = new InfoService();
    ProfileService profileService = new ProfileService();
    ContentService contentService = new ContentService();

    //setup
    Info i = new Info("info1", "p1");
    Profile p = new Profile("p1", "profile1");

    // TODO: the following part needs to be corrected
    Optional<Info> info = infoService.findByName("info1");

    if (!info.isPresent()) {
        return Optional.empty();
    } else {
         Optional<Profile> profile = profileService.findById(info.get().profileId);

         Content content;

         if (!profile.isPresent()) {
             content = contentService.createContent(info);
         } else {
             content = contentService.createContent(info, profile);
         }

        System.out.println(content);
     }
}
import java.util.Optional;
班级信息{
字符串名;
字符串profileId;
信息(字符串名称、字符串配置文件ID){
this.name=名称;
this.profileId=profileId;
}
}
班级简介{
字符串profileId;
字符串profileName;
配置文件(字符串配置文件ID、字符串配置文件名称){
this.profileId=profileId;
this.profileName=profileName;
}
}
课堂内容{
字符串信息名;
字符串profileName;
内容(字符串信息名、字符串配置文件名){
this.infoName=infoName;
this.profileName=profileName;
}
public java.lang.String-toString()文件{
返回“Content{“+”infoName='”+infoName+'\''+'”,profileName='“+profileName+'\''+'}”;
}
}
类信息服务{
可选findByName(字符串名称){//todo实现}
}
类配置文件服务{
可选findById(字符串id){//todo实现}
}
类内容服务{
内容createContent(信息i,配置文件p){
返回新内容(i.name,p.profileName);
}
内容创建内容(信息一){
返回新内容(即名称,空);
}
}
公共静态void main(字符串[]args){
InfoService InfoService=newinfoservice();
ProfileService ProfileService=新的ProfileService();
ContentService=newcontentservice();
//设置
信息i=新信息(“信息1”、“p1”);
剖面p=新剖面(“p1”、“剖面1”);
//TODO:以下部分需要更正
可选信息=infoService.findByName(“info1”);
如果(!info.isPresent()){
返回可选的.empty();
}否则{
可选配置文件=profileService.findById(info.get().profileId);
内容;
如果(!profile.isPresent()){
content=contentService.createContent(info);
}否则{
content=contentService.createContent(信息、配置文件);
}
系统输出打印项次(内容);
}
}

我对Java可选项的理解是减少
if null
检查,但如果没有
if
检查,我仍然无法做到这一点。是否有更好的解决方案来使用
map
flatMap
,并拥有简洁的代码?

这是您能得到的最好的解决方案<代码>映射仅在lambda存在时执行
orElseGet
仅在lambda未执行时执行

return infoService.findByName("info1")
    .map(info ->
        profileService.findById(info.profileId)
            .map(profile -> contentService.createContent(info, profile))
            .orElseGet(() -> contentService.createContent(info))
    );

这是你能得到的最好的<代码>映射仅在lambda存在时执行
orElseGet
仅在lambda未执行时执行

return infoService.findByName("info1")
    .map(info ->
        profileService.findById(info.profileId)
            .map(profile -> contentService.createContent(info, profile))
            .orElseGet(() -> contentService.createContent(info))
    );

返回类型中存在一个
if-else
矛盾。问题的简单性与示例的复杂性不匹配。请重新措辞(如果这确实是一个复杂的问题)或将示例简化为MCVE@nullpointer你能解释一下你的意思吗?这段代码是我正在做的事情的重写(我不想使用公司代码库中使用的相同模型)
if(!info.isPresent()){return Optional.empty();}
我指的是void方法中的这部分,返回类型中存在一个
if-else
矛盾。问题的简单性与示例的复杂性不匹配。请重新措辞(如果这确实是一个复杂的问题)或将示例简化为MCVE@nullpointer你能解释一下你的意思吗?这段代码是对我正在做的事情的重写(我不想使用公司代码库中使用的相同模型)
if(!info.isPresent()){return Optional.empty();}
我的意思是这部分在void方法中,很好,Michael。有没有办法在开始时使用
map
flatMap
链接
infoService.findByName()
,使代码更加简洁?@bak是的,只需要另一个
map
调用。编辑了我的答案很好,迈克尔。有没有办法在开始时使用
map
flatMap
链接
infoService.findByName()
,使代码更加简洁?@bak是的,只需要另一个
map
调用。编辑了我的答案