Java Arrays.parallelPrefix() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 8+

Definition

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
CallWhat 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

ParameterDescription
arrayThe array to scan in place. Must not be null.
fromIndex, toIndex (optional)Range to scan: fromIndex inclusive, toIndex exclusive.
opA side-effect-free, associative function that combines two values. Must not be null.

Return Value

Return ValueDescription
voidNothing. Each element is replaced by the cumulative result up to and including it.

Compare with Similar Methods

Related array operations:

MethodHow it differs
Arrays.parallelSetAll(a, f)Each element computed from its index alone, not from earlier elements.
Arrays.stream(a).sum()Only the final total, not every running value.
a for loopa[i] += a[i - 1] does a sequential prefix sum.
Arrays.parallelSort(a)Another parallel in-place operation.

Java Examples

Example 1 — Running Totals and the Highest Value So Far

Turn daily sales into running totals, and daily temperatures into a running maximum.

Java
import java.util.Arrays;

public class ArraysParallelPrefixDemo {
    public static void main(String[] args) {
        int[] sales = {1, 2, 3, 4, 5};
        Arrays.parallelPrefix(sales, Integer::sum);
        System.out.println("running total:  " + Arrays.toString(sales));

        int[] temps = {3, 7, 5, 9, 2};
        Arrays.parallelPrefix(temps, Math::max);
        System.out.println("highest so far: " + Arrays.toString(temps));
    }
}

How It Works

  1. Method reference as operator. Integer::sum fits IntBinaryOperator: it takes two int values and returns their sum.
  2. Each slot adds the previous total. 1, then 1 + 2 = 3, 3 + 3 = 6, 6 + 4 = 10, and 10 + 5 = 15; the last element is the grand total.
  3. Running maximum. With Math::max, each slot holds the largest temperature up to that day: 7 stays 7 when the next day is 5.
  4. Associative operators. Sum and max give the same result however the work is grouped, which is why the parallel scan is safe for them.

Example 2 — Factorials and a Scan over a Range

Use a long[] for running products, then scan only the middle of an int[].

Java
import java.util.Arrays;

public class ArraysParallelPrefixRange {
    public static void main(String[] args) {
        long[] f = {1, 2, 3, 4, 5, 6};
        Arrays.parallelPrefix(f, (x, y) -> x * y);
        System.out.println("factorials:  " + Arrays.toString(f));

        int[] part = {10, 1, 1, 1, 10};
        Arrays.parallelPrefix(part, 1, 4, (x, y) -> x + y);
        System.out.println("middle only: " + Arrays.toString(part));
    }
}

How It Works

  1. Running product. Multiplying as you go turns 1, 2, 3, 4, 5, 6 into 1!, 2!, 3!, ... 6! = 720.
  2. Room to grow. Factorials outgrow int quickly, so the array is long[] and the lambda is a LongBinaryOperator.
  3. Range scan. Only indexes 1, 2, and 3 take part: 1, 1 + 1 = 2, 2 + 1 = 3.
  4. 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);
        }
    }
}

How It Works

  1. Object overload. parts is a String[], so the operator is a BinaryOperator<String>.
  2. concat as the operator. String::concat means (x, y) -> x.concat(y), which joins two strings.
  3. 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.
  4. 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];

Next: Arrays.parallelSetAll()

Compute every element from its index, in parallel.

parallelSetAll() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful