I am testing out the new Stream
API in java-8 and want to check the outcome of 10000 random coinflips. So far I have:
public static void main(String[] args) {
Random r = new Random();
IntStream randomStream = r.ints(10000,0, 2);
System.out.println("Heads: " + randomStream.filter(x -> x==1).count());
System.out.println("Tails: " + randomStream.filter(x -> x==0).count());
}
but this throws the exception:
java.lang.IllegalStateException: stream has already been operated upon or closed
I understand why this is happenning but how can i print the count for heads and tails if I can only use the stream once?