If you really want to parallelize part1 and part2 (for example, your BigCollection has very few elements, fewer processor cores), you can do the following trick. Suppose you have two methods part1 and part2 in the current class:
public long part1(Type t) { ... } public long part2(Type t) { ... }
Create a stream of two functions created from these methods and process it in parallel:
bigCollection.parallelStream() .map(e -> Stream.<ToLongFunction<Type>>of(this::part1, this::part2) .parallel() .mapToLong(fn -> fn.applyAsLong(e)).reduce(1, (a, b) -> a*b)) .// continue the outer stream operations
However, this is a very rare case. Since @PeterLawrey noted that your external collection is large enough, you do not need to parallelize part1 and part2 . Instead, you will process individual elements in parallel.
source share