如何计算流过滤器上的匹配项?
How to count matches on a stream filter?
如何计算流过滤器的匹配项?我正在尝试将以下代码重构为 java8 stream
:
//java7
int i = 0;
for (Node node : response.getNodes()) {
Integer id = node.getId();
if (id != null) {
node.setContent("This is the id: " + id);
i++;
}
}
//java8
response.getNodes().stream()
.filter(node -> node.getId() != null)
.forEach(node -> node.setValue("This is the id: " + node.getId()));
我现在如何获取已应用的已过滤元素的数量?
附带问题:在旧代码中,我可以多次重复使用 Integer id
。我怎样才能通过流实现同样的效果?
因为setValue
是一个副作用函数,你可以使用peek
:
long i = response.getNodes()
.stream()
.filter(node -> node.getId() != null)
.peek(node -> node.setValue("This is the id: " + node.getId()))
.count();
我不喜欢这种方法,因为 peak 用于调试目的(这可以解决问题)。请注意,在 Java 9 中,如果 count()
可以直接从源计算计数,则它可能无法执行流管道(我认为这里不是这种情况,因为您应用了过滤但记住它是很好的)。
Sidequestion: in the old code I can reuse the Integer id multiple
times. How can I achieve the same with streams?
这取决于您的用例,因为 API 没有元组,您最好的机会是创建一个 class,比方说 Tuple2
,这样您就可以将每个节点映射到一个新元组并重复使用 id。
类似于:
.stream().map(node -> new Tuple2<>(node, node.getId()).moreStreamOps(...);
^
|
at that point you have a Stream<Tuple2<Node, Integer>>
from which you can grab the id with Tuple2#getSecond
在你的情况下,如果你留在节点流中,你可以随时使用 getId()
获取 id。
如何计算流过滤器的匹配项?我正在尝试将以下代码重构为 java8 stream
:
//java7
int i = 0;
for (Node node : response.getNodes()) {
Integer id = node.getId();
if (id != null) {
node.setContent("This is the id: " + id);
i++;
}
}
//java8
response.getNodes().stream()
.filter(node -> node.getId() != null)
.forEach(node -> node.setValue("This is the id: " + node.getId()));
我现在如何获取已应用的已过滤元素的数量?
附带问题:在旧代码中,我可以多次重复使用 Integer id
。我怎样才能通过流实现同样的效果?
因为setValue
是一个副作用函数,你可以使用peek
:
long i = response.getNodes()
.stream()
.filter(node -> node.getId() != null)
.peek(node -> node.setValue("This is the id: " + node.getId()))
.count();
我不喜欢这种方法,因为 peak 用于调试目的(这可以解决问题)。请注意,在 Java 9 中,如果 count()
可以直接从源计算计数,则它可能无法执行流管道(我认为这里不是这种情况,因为您应用了过滤但记住它是很好的)。
Sidequestion: in the old code I can reuse the Integer id multiple times. How can I achieve the same with streams?
这取决于您的用例,因为 API 没有元组,您最好的机会是创建一个 class,比方说 Tuple2
,这样您就可以将每个节点映射到一个新元组并重复使用 id。
类似于:
.stream().map(node -> new Tuple2<>(node, node.getId()).moreStreamOps(...);
^
|
at that point you have a Stream<Tuple2<Node, Integer>>
from which you can grab the id with Tuple2#getSecond
在你的情况下,如果你留在节点流中,你可以随时使用 getId()
获取 id。