如何在可选流中映射列表中索引为 0 的值

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

How to map value at index 0 for a list in an Optional Stream

问题

以下是翻译好的部分:

我有以下的方法,目前它正常工作。我尝试着完成所有事情并在不需要执行额外的 if 检查的情况下获取 Optional 流中的值。是否可以进行映射并在索引 0 处获取 Result 对象?请给予建议,谢谢。

public String getData(HttpEntity<Request> request, String endPoint) {
    ResponseEntity<Reponse> response =
        template.exchange(endPoint, HttpMethod.POST, request, Reponse.class);

    List<Result> results = Optional.ofNullable(response)
        .map(ResponseEntity::getBody)
        .map(Response::getQueryResult)
        .map(QueryResult::getResults)
        // getResults 是 Result 对象的 ArrayList。我能否在此处获取索引 0 处的 Result 对象?
        // 接下来我计划在此处使用 .map(Result::getValue)。
        .orElse(null);
    if (CollectionUtils.isNotEmpty(results)) {
        return results.get(0).getValue();
    }
    return null;
}
英文:

I have the following method which is working fine. I am trying to accomplish everything and get the value inside that Optional stream
without having to do the additional if check. Is it possible to map and get the Result object at index 0? Please advice thanks.

public String getData(HttpEntity&lt;Request&gt; request, String endPoint){
    ResponseEntity&lt;Reponse&gt; response = 
        template.exchange(endPoint, HttpMethod.POST, request, Reponse.class);

    List&lt;Result&gt; results = Optional.ofNullable(response)
        .map(ResponseEntity::getBody)
        .map(Response::getQueryResult)
        .map(QueryResult::getResults)
        // getResults is an ArrayList of Result Objects. Could I get the Result Object at index 0 here? 
        // following that I plan to go .map(Result::getValue) here. 
        .orElse(null);
    if(CollectionUtils.isNotEmpty(results)){
        return results.get(0).getValue();
    }
    return null;
}

答案1

得分: 5

return Optional.ofNullable(response)
               .map(ResponseEntity::getBody)
               .map(Response::getQueryResult)
               .map(QueryResult::getResults)
               .filter(CollectionUtils::isNotEmpty)
               .map(list -> list.get(0)) // 不喜欢这部分 :)
               .map(Result::getValue)
               .orElse(null);

如果你喜欢方法引用并且认为Lambda表达式很无聊

return Optional.ofNullable(response)
               .map(ResponseEntity::getBody)
               .map(Response::getQueryResult)
               .map(QueryResult::getResults)
               .filter(CollectionUtils::isNotEmpty)
               .map(List::iterator)
               .map(Iterator::next)
               .map(Result::getValue)
               .orElse(null);

我展示这个例子是为了教育目的尽管我喜欢它但并不提倡它
英文:
return Optional.ofNullable(response)
               .map(ResponseEntity::getBody)
               .map(Response::getQueryResult)
               .map(QueryResult::getResults)
               .filter(CollectionUtils::isNotEmpty)
               .map(list -&gt; list.get(0)) // hate this part :)
               .map(Result::getValue)
               .orElse(null);

If you are a fan of method references, and you find lambdas boring

return Optional.ofNullable(response)
               .map(ResponseEntity::getBody)
               .map(Response::getQueryResult)
               .map(QueryResult::getResults)
               .filter(CollectionUtils::isNotEmpty)
               .map(List::iterator)
               .map(Iterator::next)
               .map(Result::getValue)
               .orElse(null);

I showed it for educational reasons, I don't advocate it even though I like it.

答案2

得分: 2

假设ArrayList永远不会为null

.flatMap(r -> r.stream().findFirst())

这将获取列表,将其转为流,获取带有第一个元素的Optional(如果列表为空,则为一个空的Optional)。最后,因为Optional<Optional<Result>>并不是特别有用,我们使用flatMap而不是map来将其合并为一个Optional<Result>

英文:

Assuming that the ArrayList is never null:

.flatMap(r -&gt; r.stream().findFirst())

This takes the list, streams it, gets an Optional with the first element (or an empty Optional if the list is empty. Lastly, since an Optional&lt;Optional&lt;Result&gt;&gt; isn't that useful, we use flatMap instead of map to collapse it into an Optional&lt;Result&gt;.

答案3

得分: 2

orElse更改为返回一个空的List,并从那里开始stream。这样你可以安全地调用findFirst - 对于一个空的List,它将返回Optional::empty,然后 - 你可以将其映射(如果你有的话)到Result::getValue,或者,如果这样的List不存在 - null,所以它正好符合你的程序流。

...
   .map(QueryResult::getResults)
   .orElse(Collections.emptyList())
   .stream()
   .findFirst()
   .map(Result::getValue)
   .orElse(null);
英文:

change that orElse to return an empty List and stream from there. In such a way you can invoke findFirst safely - as for an empty List it will return Optional::empty and from there - you either map it (in case you do have it) to Result::getValue or, in case such a List is not present - null, so it is exactly as your program flow.

...
   .map(QueryResult::getResults)
   .orElse(Collections.emptyList())
   .stream()
   .findFirst()
   .map(Result::getValue)
   .orElse(null);

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

发表评论

匿名网友

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

确定