In Java, Arrays.parallelPrefix(array, op) performs a prefix scan in place: element 0 stays as it is, element 1 becomes op(a[0], a[1]), element 2 becomes op(op(a[0], a[1]), a[2]), and so on. With addition, {1, 2, 3, 4} becomes {1, 3, 6, 10}.
Despite each result depending on the previous ones, the JDK can compute the scan on several threads for large arrays, because it regroups the operations. That only gives the right answer when the operator is associative ((x op y) op z equals x op (y op z)), like sum, product, max, min, or string concatenation.
A static method of java.util.Arrays (Java 8+): call it on the class, as in Arrays.parallelPrefix(...); there is no Arrays object to create. It was added in Java 8.
Syntax
Pass the array and an associative operator; a range form is also available.
Java
int[] a = {1, 2, 3, 4};
Arrays.parallelPrefix(a, Integer::sum); // [1, 3, 6, 10]
int[] b = {3, 1, 4, 1};
Arrays.parallelPrefix(b, Math::max); // [3, 3, 4, 4]
Arrays.parallelPrefix(a, 1, 3, (x, y) -> x * y); // only indexes 1 and 2
Call
What it does
Arrays.parallelPrefix(int[] a, op)
IntBinaryOperator; also long[] and double[].
Arrays.parallelPrefix(a, from, to, op)
Scans only indexes from to to - 1.
Arrays.parallelPrefix(T[] a, op)
BinaryOperator<T> for object arrays.
Arrays.parallelPrefix(a, Math::max)
Running maximum: the largest value seen so far.
Overloads exist for int[], long[], double[], and T[], each with a range form, for eight in total. There is no sequential Arrays.prefix; for small arrays a simple loop does the same job.
Library declarations
Java
// java.util.Arrays (since Java 8)
public static void parallelPrefix(int[] array, IntBinaryOperator op)
public static void parallelPrefix(int[] array, int fromIndex, int toIndex, IntBinaryOperator op)
public static void parallelPrefix(long[] array, LongBinaryOperator op)
public static void parallelPrefix(double[] array, DoubleBinaryOperator op)
public static <T> void parallelPrefix(T[] array, BinaryOperator<T> op)
public static <T> void parallelPrefix(T[] array, int fromIndex, int toIndex, BinaryOperator<T> op)
// long[] and double[] also have range forms
Parameters
Parameter
Description
array
The array to scan in place. Must not be null.
fromIndex, toIndex (optional)
Range to scan: fromIndex inclusive, toIndex exclusive.
op
A side-effect-free, associative function that combines two values. Must not be null.
Return Value
Return Value
Description
void
Nothing. Each element is replaced by the cumulative result up to and including it.
Running product. Multiplying as you go turns 1, 2, 3, 4, 5, 6 into 1!, 2!, 3!, ... 6! = 720.
Room to grow. Factorials outgrow int quickly, so the array is long[] and the lambda is a LongBinaryOperator.
Range scan. Only indexes 1, 2, and 3 take part: 1, 1 + 1 = 2, 2 + 1 = 3.
Edges unchanged. The 10s at index 0 and index 4 are outside the range, so they are neither changed nor added in.
Example 3 — Build File Paths Step by Step
Concatenate path parts so each slot holds the path up to that folder.
Java
import java.util.Arrays;
public class ArraysParallelPrefixPaths {
public static void main(String[] args) {
String[] parts = {"home", "/docs", "/java", "/notes.txt"};
Arrays.parallelPrefix(parts, String::concat);
for (String p : parts) {
System.out.println(p);
}
}
}
Output
home
home/docs
home/docs/java
home/docs/java/notes.txt
How It Works
Object overload.parts is a String[], so the operator is a BinaryOperator<String>.
concat as the operator.String::concat means (x, y) -> x.concat(y), which joins two strings.
Order is kept. Concatenation is associative but not commutative, and the prefix scan always combines in index order, so the paths come out left to right.
Every level saved. Each element now holds the full path down to that level, ending with the path to notes.txt.
Frequently Asked Questions
It replaces each element with the result of combining all elements up to it, using the operator you pass, for example running sums with Integer::sum.
No. It returns void and changes the array in place.
The parallel algorithm regroups the operations. With a non-associative operator such as subtraction, the result can differ from a simple loop.
For integer operators like sum, max, and product, yes. For double arithmetic, results may differ slightly from a sequential loop because floating-point addition is not perfectly associative.
No. For small arrays, write a loop such as for (int i = 1; i < a.length; i++) a[i] += a[i - 1];