如何使用Java 8流迭代嵌套for循环引用父元素?
发布时间:2020-12-14 06:01:18 所属栏目:Java 来源:网络整理
导读:我想使用 java8流迭代嵌套列表,并在第一次匹配时提取列表的一些结果. 不幸的是,如果子元素与过滤器匹配,我还必须从父内容中获取值. 我怎么能这样做? // java7 Result result = new Result();//find first match and pupulate the result object.for (FirstN
我想使用
java8流迭代嵌套列表,并在第一次匹配时提取列表的一些结果.
不幸的是,如果子元素与过滤器匹配,我还必须从父内容中获取值. 我怎么能这样做? // java7 Result result = new Result(); //find first match and pupulate the result object. for (FirstNode first : response.getFirstNodes()) { for (SndNode snd : first.getSndNodes()) { if (snd.isValid()) { result.setKey(first.getKey()); result.setContent(snd.getContent()); return; } } } // java8 response.getFirstNodes().stream() .flatMap(first -> first.getSndNodes()) .filter(snd -> snd.isValid()) .findFirst() .ifPresent(???); //cannot access snd.getContent() here 解决方法
当您需要两个值并且想要使用flatMap时(根据需要执行像findFirst这样的短路操作时),您必须映射到包含两个值的对象
response.getFirstNodes().stream() .flatMap(first->first.getSndNodes().stream() .map(snd->new AbstractMap.SimpleImmutableEntry<>(first,snd))) .filter(e->e.getValue().isValid()) .findFirst().ifPresent(e-> { result.setKey(e.getKey().getKey()); result.setContent(e.getValue().getContent()); }); 为了仅使用标准类,我使用Map.Entry作为Pair类型,而真正的Pair类型可能看起来更简洁. 在此特定用例中,您可以将筛选器操作移动到内部流 response.getFirstNodes().stream() .flatMap(first->first.getSndNodes().stream() .filter(snd->snd.isValid()) .map(snd->new AbstractMap.SimpleImmutableEntry<>(first,snd))) .findFirst().ifPresent(e-> { result.setKey(e.getKey().getKey()); result.setContent(e.getValue().getContent()); }); 它具有整洁的效果,只有一个匹配的项目,一个Map.Entry实例将被创建(好吧,应该作为the current implementation is not as lazy as it should,但即便如此,它仍然会创建比第一个变体更少的对象). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |