【问题标题】:Java Optional<List<T>> to Map conversionJava Optional<List<T>> 到 Map 的转换
【发布时间】:2020-09-04 14:36:00
【问题描述】:

我有一段这样的代码:

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

    }

我不想返回List&lt;Pair&lt;String, Details&gt;&gt;,而是想返回Map&lt;String, Details&gt;

如何将Optional&lt;List&lt;JobExecution&gt;&gt; 转换为键为id 且值为Detail 对象的映射?

【问题讨论】:

  • 你试过映射它吗?使用 lambda 你可以做 .map(element -> element.getField,getOtherfield)
  • 我只使用列表,但不确定使用可选列表的效果
  • 我正在查看您注释掉的代码。我想这就是你对 List 所做的吗?我想你可以寻找一个 Collect(toMap()) 选项。或者你可以把它当作一个列表,然后把这个列表变成一张地图?

标签: java java-stream optional


【解决方案1】:

您可以使用Collector.toMap作为地图收集

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

【讨论】:

    【解决方案2】:

    现有答案表明您可以使用Collectors.toMap,但另外根据标签,您不应在当前上下文中使用Optional

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

    【讨论】:

    • 好收获!在要求从Optional&lt;List&lt;T&gt;&gt; 转换的问题和标题中,也许这就是为什么我错过了 OP 创建可选内部函数的原因。
    • @Rono 从数据表示为Optional&lt;List&lt;T&gt;&gt; 的那一刻起,它实际上是有问题的。必须有一个很好的理由以这种形式表示它。 (我还没有在我们的项目中体验过这种需求。)
    • 刚刚意识到我也不使用Optional&lt;List&lt;T&gt;&gt; 意味着列表可选
    【解决方案3】:

    如果您的列表为空,只需返回空列表并继续流式传输一个空列表。

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

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        猜你喜欢
        • 2021-04-25
        • 1970-01-01
        • 1970-01-01
        • 2021-11-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多