Java可选<;列表<;T>&燃气轮机;映射转换

Java可选<;列表<;T>&燃气轮机;映射转换,java,java-stream,optional,Java,Java Stream,Optional,我有这样一段代码: public Map<String, Details> getAllDetails(final String name) { Optional<List<JobExecution>> allJobExecutionsByName = Optional.ofNullable(jobExecutionDao.getAllJobExecutionsByName(name)); // return allJo

我有这样一段代码:

    public Map<String, Details> getAllDetails(final String name) {
        Optional<List<JobExecution>> allJobExecutionsByName = Optional.ofNullable(jobExecutionDao.getAllJobExecutionsByName(name));

//        return allJobExecutionsByName.map(x -> x.stream()
//                                                    .map(execution -> Pair.of(getJobParam(execution, "id"), getDetailsFromExecution(execution)))
//                                                    .collect(toList()))
//                                         .orElse(emptyList());

    }
    public Map<String, Details> getAllDetails(final String name) {
        return Optional.ofNullable(jobExecutionDao.getAllJobExecutionsByName(name))
                .map(x -> x.stream()
                .collect(Collector.toMap(e -> getJobParam(e, "id"), e -> getDetailsFromExecution(e)))
                .orElse(Collections.emptyMap());
    }
publicmap getAllDetails(最终字符串名){
可选allJobExecutionsByName=Optional.ofNullable(jobExecutionDao.getAllJobExecutionsByName(名称));
//返回allJobExecutionsByName.map(x->x.stream()
//.map(execution->Pair.of(getJobParam(execution,“id”)、getDetailsFromExecution(execution)))
//.collect(toList()))
//.orElse(emptyList());
}
我不想返回
List
,而是想返回
Map


如何将
可选
转换为键为
id
且值为
详细信息
对象的映射?

您可以使用
收集器.toMap
作为映射进行收集

return allJobExecutionsByName.map(x -> 
          x.stream()
           .collect(Collector.toMap(e -> getJobParam(e, "id"),
                                    e -> getDetailsFromExecution(e))))
       .orElse(Collections.emptyMap());

现有答案建议您可以使用
Collectors.toMap
,但根据标记,您不应在当前上下文中使用
可选的

public Map<String, Details> getAllDetails(final String name) {
    List<JobExecution>> allJobExecutionsByName = jobExecutionDao.getAllJobExecutionsByName(name);
    
    // perform below check only if you cannot control the returned value above
    if(allJobExecutionsByName == null) return Collections.emptyMap();
    
    return allJobExecutionsByName.stream()
                .collect(Collector.toMap(e -> getJobParam(e, "id"),
                                     e -> getDetailsFromExecution(e))));
}
publicmap getAllDetails(最终字符串名){
List>allJobExecutionsByName=jobExecutionDao.getAllJobExecutionsByName(名称);
//仅当无法控制上面的返回值时,才执行下面的检查
if(allJobExecutionsByName==null)返回Collections.emptyMap();
返回allJobExecutionsByName.stream()
.collect(Collector.toMap(e->getJobParam(e,“id”),
e->getDetailsFromExecution(e));
}

如果您的列表为空,只需返回空列表并继续传输空列表即可

allJobExecutionsByName.orElse(Collections.emptyList())
    .stream()
    .collect(Collectors.toMap(
        (e -> getJobParam(e, "id"),
        e -> getDetailsFromExecution(e))
    ));

我遇到了这个问题,所以我做了这样的事情:

    public Map<String, Details> getAllDetails(final String name) {
        Optional<List<JobExecution>> allJobExecutionsByName = Optional.ofNullable(jobExecutionDao.getAllJobExecutionsByName(name));

//        return allJobExecutionsByName.map(x -> x.stream()
//                                                    .map(execution -> Pair.of(getJobParam(execution, "id"), getDetailsFromExecution(execution)))
//                                                    .collect(toList()))
//                                         .orElse(emptyList());

    }
    public Map<String, Details> getAllDetails(final String name) {
        return Optional.ofNullable(jobExecutionDao.getAllJobExecutionsByName(name))
                .map(x -> x.stream()
                .collect(Collector.toMap(e -> getJobParam(e, "id"), e -> getDetailsFromExecution(e)))
                .orElse(Collections.emptyMap());
    }
publicmap getAllDetails(最终字符串名){
返回可选的.ofNullable(jobExecutionDao.getAllJobExecutionsByName(名称))
.map(x->x.stream()
.collect(Collector.toMap(e->getJobParam(e,“id”),e->getDetailsFromExecution(e)))
.orElse(Collections.emptyMap());
}

你试过在它上面映射吗?使用lambda,你可以这样做。map(element->element.getField,getOtherfield)我只在列表上工作,但不确定它在可选列表上会是什么样子。我正在查看你注释掉的代码。我想这就是你在列表上所做的。我想你可以找一个Collect(toMap())选项。或者你可以把它作为一个列表,并将该列表转换为一个地图?很好的回答!在问题和标题中,要求从
可选
转换,这可能是我错过创建可选内部函数的那部分的原因。@Rono从数据表示为
可选
时开始,它实际上是有问题的。必须有一个v用这样的形式表示它是非常好的理由。(我还没有在我们的项目中体验到这种需要。)刚刚意识到我也不使用for
Optional
表示list的Optional