如何使用Java 8流迭代嵌套for循环引用父元素?
我想使用java8 streams
迭代嵌套列表,并在第一次匹配时提取列表的一些结果。 不幸的是,如果子元素与filter匹配,我还必须从父内容中获取值。
我怎么能这样做?
// 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
实例将被创建(好吧,因为当前的实现不像它应该的那样懒,但即使这样它仍然会创建比第一个变体更少的对象)。
它应该是这样的:
编辑:感谢Holger指出代码不会停在第一个有效的FirstNode上
response.getFirstNodes().stream() .filter(it -> {it.getSndNodes().stream().filter(SndNode::isValid).findFirst(); return true;}) .findFirst() .ifPresent(first -> first.getSndNodes().stream().filter(SndNode::isValid).findFirst().ifPresent(snd -> { result.setKey(first.getKey()); result.setContent(snd.getContent()); }));
可以在这里找到测试