迭代嵌套集合以查找与条件匹配的第一个子子元素 [英] Iterating through nested collections to find first sub-sub-sub element matching a criterion

查看:220
本文介绍了迭代嵌套集合以查找与条件匹配的第一个子子元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个其他对象列表的对象,每个其他对象有一个列表等。我需要在层次结构中找到层次结构中具有匹配某个值的属性的第一个(和唯一)最后一个元素。看到我现在的代码会更清楚:

$ o $ {code> @Override
public Poste findByNumeroAndMillesime(String numero,Millesime millesime){
返回millesime
.getDivisions()
.stream()
.filter(
除法 - >除法
.getGroupes()
。 stream()
.filter(
groupe - > groupe
.getClasses()
.stream()
.filter(
classe - > ()
.getSousClasses()
.stream()
.filter(
sousClasse - > sousClasse
.getPostes()
.stream()
.filter(poste - > numero.equals(poste.getNumero()))
.findFirst()
.get()
))));




$ p
$ b

我需要返回与作为参数传递。



在此先感谢您。

解决方案

可以像这样尝试flatMap:

 可选< Postes> first = 
millesime.getDivisions()
.stream()
.flatMap(m - > m.getGroupes()。stream())
.flatMap(m - > ; m.getClasses()。stream())
.flatMap(m - > m.getSousClasses()。stream())
.flatMap(m - > m.getPostes() ())
.filter(postes - > numero.equals(postes.getNumero()))
.findFirst();

但是,如果您有巨大的树,请注意您可能遇到的问题,因为flatMap并非完全懒惰。见:




I have an object with a List of other objects, each other object has a List etc. I need to find in the hierarchy the first (and unique) last element in the hierarchy that has a property matching some value. Seeing my present code will be clearer :

    @Override
public Poste findByNumeroAndMillesime(String numero, Millesime millesime) {
    return millesime
            .getDivisions()
            .stream()
            .filter(
                    division -> division
                    .getGroupes()
                    .stream()
                    .filter(
                          groupe -> groupe
                          .getClasses()
                          .stream()
                          .filter(
                                  classe -> classe
                                  .getSousClasses()
                                  .stream()
                                  .filter(
                                          sousClasse -> sousClasse
                                          .getPostes()
                                          .stream()
                                          .filter(poste -> numero.equals(poste.getNumero()))
                                          .findFirst()
                                          .get()
                                          ))));

}

I need to return the Poste having the same numero as that passed as a parameter.

Thanks in advance.

解决方案

You could try flatMap like this:

Optional<Postes> first = 
        millesime.getDivisions()
              .stream()
              .flatMap(m -> m.getGroupes().stream())
              .flatMap(m -> m.getClasses().stream())
              .flatMap(m -> m.getSousClasses().stream())
              .flatMap(m -> m.getPostes().stream())
              .filter(postes -> numero.equals(postes.getNumero()))
              .findFirst();

But be aware of issues you may encounter if you have huge tree, as flatMap is not completly lazy. See:

这篇关于迭代嵌套集合以查找与条件匹配的第一个子子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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