Java Optional> 转换为 Map

huangapple go评论72阅读模式
英文:

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&lt;String, Details&gt; getAllDetails(final String name) {
        Optional&lt;List&lt;JobExecution&gt;&gt; allJobExecutionsByName = Optional.ofNullable(jobExecutionDao.getAllJobExecutionsByName(name));

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

    }

Instead of returning List&lt;Pair&lt;String, Details&gt;&gt;, I want to return Map&lt;String, Details&gt;

How can I convert Optional&lt;List&lt;JobExecution&gt;&gt; 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 -&gt; 
          x.stream()
           .collect(Collector.toMap(e -&gt; getJobParam(e, &quot;id&quot;),
                                    e -&gt; 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&lt;String, Details&gt; getAllDetails(final String name) {
    List&lt;JobExecution&gt;&gt; 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 -&gt; getJobParam(e, &quot;id&quot;),
                                     e -&gt; 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 -&gt; getJobParam(e, &quot;id&quot;),
        e -&gt; 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&lt;String, Details&gt; getAllDetails(final String name) {
        return Optional.ofNullable(jobExecutionDao.getAllJobExecutionsByName(name))
                .map(x -&gt; x.stream()
                .collect(Collector.toMap(e -&gt; getJobParam(e, &quot;id&quot;), e -&gt; getDetailsFromExecution(e)))
                .orElse(Collections.emptyMap());
    }

huangapple
  • 本文由 发表于 2020年9月4日 22:36:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63743221.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定