package streams;

import java.util.OptionalDouble;

import java.util.stream.IntStream;

public class LargeRange {

    public static void main(String[] args) {

        long start = System.currentTimeMillis();

        OptionalDouble result = IntStream.range(0, Integer.MAX_VALUE)

.parallel()

.filter(i -> i % 2 == 0)

.average();

        System.out.printf("Parallel version took %d\n",

 System.currentTimeMillis()-start);

        start = System.currentTimeMillis();

        OptionalDouble result2 = IntStream.range(0,

              Integer.MAX_VALUE)

.filter(i -> i % 2 == 0)

.average();

        System.out.printf("Serial version took %d\n",

 System.currentTimeMillis()-start);

    }

}

 

 

 

 

 

package streams;

import java.util.OptionalInt;

import java.util.stream.IntStream;

public class Associative {

    public static void main(String[] args) {

        OptionalInt result =  IntStream.range(1, 1000)

             .parallel()

             .reduce((a, b) -> a-b);

        System.out.println(result.getAsInt());

        result =  IntStream.range(1, 1000)

              .reduce((a, b) -> a-b);

        System.out.println(result.getAsInt());

    }

}

 

 

 

 

 

package streams;

 

import java.util.stream.IntStream;

 

public class Associative2 {

 

    public static void main(String[] args) {

        System.out.println("Running in parallel");

        IntStream.range(1, 1000)

          .parallel()

          .filter(a -> a %2 == 1)

          .limit(10)

          .sorted()

         .forEach(a -> System.out.println(a));

        System.out.println("Running in serial");

        IntStream.range(1, 1000)

         .filter(a -> a %2 == 1)

         .limit(10)

         .forEach(a -> System.out.println(a));

    }

}