Can you split a stream into two streams?

前端 未结 10 837
栀梦
栀梦 2020-11-27 12:03

I have a data set represented by a Java 8 stream:

Stream stream = ...;

I can see how to filter it to get a random subset - for exa

10条回答
  •  情书的邮戳
    2020-11-27 12:35

    Not exactly. You can't get two Streams out of one; this doesn't make sense -- how would you iterate over one without needing to generate the other at the same time? A stream can only be operated over once.

    However, if you want to dump them into a list or something, you could do

    stream.forEach((x) -> ((x == 0) ? heads : tails).add(x));
    

提交回复
热议问题