如何使用peek或anyMatch返回对象而不是流 [英] How to return object instead of stream using peek or anyMatch

查看:149
本文介绍了如何使用peek或anyMatch返回对象而不是流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码:

boolean anyMatch = ifxStopChkInqRs.getBankSvcRs().stream().anyMatch(
  b -> b.getStopChkInqRs().stream().anyMatch(
    i -> i.getStopChkRec().stream()
      .filter(d -> d.getStopChkInfo().getDesc().equals("D"))
      .findFirst()
      .isPresent()));

这将正确返回true/false值,并在找到的第一个对象处退出.

This returns a true/false value correctly, and quits at the first object it finds if any.

但是,如何返回对象本身,在这种情况下,该对象的类型为StopChkRec-i对象?我将anyMatch都更改为peek,并在findFirst()前面添加了get(),但是返回了最高级别的流-Stream<BankSvcRs>,这当然超出了整个搜索的目的.

But, how can I return the object itself, which in this case would be of type StopChkRec - the i object? I changed both anyMatch to peek and a added a get() in front of findFirst(), but that returned a stream at the highest level - Stream<BankSvcRs> - which beats the whole search purpose of course.

欢迎提供任何帮助和/或重新构造此lambda表达式的方法.

Any help and/or a re-approach to re-constructing this lambda expression is welcome.

推荐答案

在这里是

    Optional<StopChkRecType> findFirst = ifxStopChkInqRs.getBankSvcRs()
        .stream()
        .flatMap(b -> b.getStopChkInqRs().stream())
        .flatMap(i -> i.getStopChkRec().stream())
        .filter(d -> d.getStopChkInfo().getDesc().equals("D"))
        .findFirst();

受到@ykaganovich启发的答案,他使我与flatMap一起走上了正确的轨道,也位于

Answer inspired by @ykaganovich who put me on the right track with flatMap, but also at this link which explains how to go deep and come out clean.

这篇关于如何使用peek或anyMatch返回对象而不是流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆