英文:
Java Optional<List<T>> to Map conversion
问题
以下是翻译好的代码部分:
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());
}
英文:
I have a snippet of code like this:
    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());
    }
Instead of returning List<Pair<String, Details>>, I want to return Map<String, Details>
How can I convert Optional<List<JobExecution>> to a Map with the key being id and value being the Detail object?
答案1
得分: 1
你可以使用Collector.toMap将数据收集成Map。
return allJobExecutionsByName.map(x ->
          x.stream()
           .collect(Collector.toMap(e -> getJobParam(e, "id"),
                                    e -> getDetailsFromExecution(e))))
       .orElse(Collections.emptyMap());
英文:
You can use Collector.toMap to collect as Map
return allJobExecutionsByName.map(x -> 
          x.stream()
           .collect(Collector.toMap(e -> getJobParam(e, "id"),
                                    e -> getDetailsFromExecution(e))))
       .orElse(Collections.emptyMap());
答案2
得分: 1
现有答案建议您可以使用Collectors.toMap,但是另外基于标签,您不应该在当前上下文中使用Optional。
public Map<String, Details> getAllDetails(final String name) {
    List<JobExecution> allJobExecutionsByName = jobExecutionDao.getAllJobExecutionsByName(name);
    // 仅在无法控制上面返回的值时执行以下检查
    if (allJobExecutionsByName == null) return Collections.emptyMap();
    return allJobExecutionsByName.stream()
            .collect(Collectors.toMap(e -> getJobParam(e, "id"),
                                      e -> getDetailsFromExecution(e)));
}
英文:
The existing answer suggests that you can use Collectors.toMap, but additionally based on the tags, you should not use Optional in your current context.
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))));
}
答案3
得分: 0
如果您的列表为空,则只需返回空列表并继续对空列表进行流处理。
allJobExecutionsByName.orElse(Collections.emptyList())
        .stream()
        .collect(Collectors.toMap(
            (e -> getJobParam(e, "id")),
            e -> getDetailsFromExecution(e))
        );
英文:
If your list is null, just return empty list and proceed streaming an empty list.
allJobExecutionsByName.orElse(Collections.emptyList())
    .stream()
    .collect(Collectors.toMap(
        (e -> getJobParam(e, "id"),
        e -> getDetailsFromExecution(e))
    ));
答案4
得分: 0
我遇到过这个问题,所以我做了类似这样的操作:
public Map<String, Details> getAllDetails(final String name) {
    return Optional.ofNullable(jobExecutionDao.getAllJobExecutionsByName(name))
            .map(x -> x.stream()
            .collect(Collectors.toMap(e -> getJobParam(e, "id"), e -> getDetailsFromExecution(e))))
            .orElse(Collections.emptyMap());
}
英文:
I went through this problem, so I did something like this:
    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());
    }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论