Skip to main content

Posts

Showing posts with the label Parallel Stream

Java 8 - Streams - Parallel Stream

Operations on parallel streams are performed concurrently on multiple threads and so are faster than sequential streams (although item order is affected)However, operations on parallel streams can be ordered using 'stream.forEachOrdered'
parallel Integer Stream
Stream<Integer> parallelIntegerStream = new ArrayList<>(Arrays.asList(1,2,3)).parallelStream();

another parallel Integer Stream
Stream<Integer> parallelIntegerStream2 = IntStream.range(0, 10).parallel().boxed(); 

parallel String Stream
Stream<String> parallelStringStream = Arrays.asList("ab","acd","db","dab","ea").parallelStream();


USAGE 1

Consumer<Stream> orderedParallelStreamConsumer = stream -> stream.forEachOrdered(item -> log.info(item.toString()));

orderedParallelStreamConsumer.accept(parallelIntegerStream);

orderedParallelStreamConsumer.accept(parallelIntegerStream2);

USAGE 2

Consumer<Stream<String>> unorderedParalle…