当前位置: 动力学知识库 > 问答 > 编程问答 >

Reusing Streams in Java 8

问题描述:

This question already has an answer here:

  • Java 8 Stream IllegalStateException: Stream has already been operated on or closed

    5 answers

网友答案:

You can only reuse a collection as it has memoriation of results.

List<Element> part1 = list.stream().filter(x -> x.getN1() < x.getN2).collect(toList());
double result = (double) part1.stream().filter(y -> y.isRight()).count() / part1.size();

A Stream is a builder for some code which is optimised at run time. It's execution isn't as dynamic as it appears.

网友答案:

Streams are not supposed to be reused, or if you want something seemed to it, you can use suppliers as mentioned here : Copy a stream to avoid "stream has already been operated upon or closed" (java 8)

分享给朋友:
您可能感兴趣的文章:
随机阅读: