JetBrains Academy: Parallel streams - Kamil-Jankowski/Learning-JAVA GitHub Wiki

JetBrains Academy: Parallel streams

Prime numbers:

You need to create a prepared parallel LongStream for filtering prime numbers in the given range (inclusive both borders). After calling the count() method it should return the count of prime numbers in the given range. Pay attention to the method template.

The static method NumberUtils.isPrime(someLong) is already available for you. It returns true if the passed value is prime and false otherwise.

import java.util.Scanner;
import java.util.stream.*;

class ParallelFilteringStream {

    public static LongStream createPrimesFilteringStream(long start, long end) {
        return LongStream.rangeClosed(start, end)
                         .parallel()
                         .filter(NumberUtils::isPrime);
    }

    /* Please do not modify the code below */
    public static void main(String[] args) {
        final Scanner scanner = new Scanner(System.in);
        final String[] vals = scanner.nextLine().split(" ");

        final long left = Long.valueOf(vals[0]);
        final long right = Long.valueOf(vals[1]);

        final LongStream stream = createPrimesFilteringStream(left, right);

        if (!stream.isParallel()) {
            throw new NoParallelStreamException(
                "You need to write a parallel stream, not sequential!");
        }

        final Long count = stream.boxed().count();

        System.out.println(count);
    }

    public static class NumberUtils {

        public static boolean isPrime(long n) {
            return n > 1 && LongStream
                    .rangeClosed(2, n - 1)
                    .noneMatch(divisor -> n % divisor == 0);
        }
    }

    static class NoParallelStreamException extends RuntimeException {

        public NoParallelStreamException(String text) {
            super(text);
        }
    }
}

Inverting streams:

There is a new challenge. Someone has not correctly chosen the types of streams in a program (parallel and sequential).

You need to implement a method that inverses the state of every stream from the given list (parallel → sequential and vice versa).

The method should return a new list of streams which are inverted.

import java.util.*;
import java.util.stream.*;

public class Main {

    private static List<LongStream> invertedStreams(List<LongStream> streams) {
        // write your code here
        return streams.stream()
                .map(elem -> elem.isParallel() ? elem.sequential() : elem.parallel())
                .collect(Collectors.toList());
    }

    /* Do not modify the code below */
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        List<Boolean> parallelFlags = Arrays
                .stream(scanner.nextLine().split("\\s+"))
                .map(Boolean::parseBoolean)
                .collect(Collectors.toList());

        // :)
        List<LongStream> streams = Stream
                .iterate(0,
                        i -> i < parallelFlags.size(),
                        i -> i + 1)
                .map(i -> {
                    var stream = LongStream.of();
                    if (parallelFlags.get(i)) {
                        stream = stream.parallel();
                    }
                    return stream;
                }).collect(Collectors.toList());

        List<String> invertedParallelFlagsAsStrings =
                invertedStreams(streams).stream()
                        .map(LongStream::isParallel)
                        .map(Object::toString)
                        .collect(Collectors.toList());

        System.out.println(String.join(" ", invertedParallelFlagsAsStrings));
    }
}

⚠️ **GitHub.com Fallback** ⚠️