One approach, being equivalent to your original code, would be:
private static <T extends Comparable<T>> List<T> doAction(List<T> original, T state) {
Map<Boolean, List<T>> collect = original.parallelStream()
.collect(Collectors.partitioningBy(e -> e.compareTo(state) < 0));
List<T> left = collect.get(true);
List<T> right = collect.get(false);
return Stream.of(left, right).flatMap(List::stream).collect(Collectors.toList());
}
However, since you only partition the data, to join them afterwards, a simpler solution would be:
private static <T extends Comparable<T>> List<T> doAction(List<T> original, T state) {
return original.parallelStream()
.sorted(Comparator.comparingInt(e -> Integer.signum(e.compareTo(state))|1))
.collect(Collectors.toList());
}
But note that in either case, you need a rather big source list to benefit from parallel execution.
Note that the second approach can be even performed in-place, in case the original List
is mutable (and you don’t need the original order anymore):
original.sort(Comparator.comparingInt(e -> Integer.signum(e.compareTo(state))|1));