Java Arrays.parallelSetAll() Method

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

Definition

In Java, Arrays.parallelSetAll(array, generator) sets every element of array to generator applied to its index, just like setAll. The difference is that the work may be split across several threads of the common Fork/Join pool.

The final contents are the same as with setAll, as long as each value depends only on its index. The order in which indexes are computed is not defined, so the generator must not depend on other elements being done or change shared state.

A static method of java.util.Arrays (Java 8+): call it on the class, as in Arrays.parallelSetAll(...); there is no Arrays object to create. It was added in Java 8 with the other parallel array methods.

Syntax

Same shape as setAll: an array and a lambda from index to value.

Java
double[] roots = new double[1_000_000];
Arrays.parallelSetAll(roots, i -> Math.sqrt(i));
int[] evens = new int[4];
Arrays.parallelSetAll(evens, i -> i * 2);   // [0, 2, 4, 6]
CallWhat it does
Arrays.parallelSetAll(int[] a, i -> ...)IntUnaryOperator generator.
Arrays.parallelSetAll(long[] a, i -> ...)IntToLongFunction generator.
Arrays.parallelSetAll(double[] a, i -> ...)IntToDoubleFunction generator.
Arrays.parallelSetAll(T[] a, i -> ...)IntFunction<? extends T> generator.

Parallelism only pays off for large arrays or expensive generators; for a few thousand cheap elements, the overhead of splitting the work can make it slower than setAll.

Library declarations
Java
// java.util.Arrays (since Java 8)
public static void parallelSetAll(int[] array, IntUnaryOperator generator)
public static void parallelSetAll(long[] array, IntToLongFunction generator)
public static void parallelSetAll(double[] array, IntToDoubleFunction generator)
public static <T> void parallelSetAll(T[] array, IntFunction<? extends T> generator)

Parameters

ParameterDescription
arrayThe array to fill. Must not be null.
generatorA side-effect-free function from index to value. Must not be null.

Return Value

Return ValueDescription
voidNothing. Every element of array is overwritten.

Compare with Similar Methods

Parallel and sequential relatives:

MethodHow it differs
Arrays.setAll(a, f)Single-threaded; the simple default for small arrays.
Arrays.parallelSort(a)Sorts with the same Fork/Join pool.
Arrays.parallelPrefix(a, op)Running totals, where each result does depend on earlier elements.
Arrays.fill(a, v)One constant value; nothing to compute.

Java Examples

Example 1 — Fill int and double Arrays from the Index

Square each index, and divide each index by two.

Java
import java.util.Arrays;

public class ArraysParallelSetAllDemo {
    public static void main(String[] args) {
        int[] squares = new int[6];
        Arrays.parallelSetAll(squares, i -> i * i);
        System.out.println(Arrays.toString(squares));

        double[] halves = new double[4];
        Arrays.parallelSetAll(halves, i -> i / 2.0);
        System.out.println(Arrays.toString(halves));
    }
}

How It Works

  1. Independent elements. Each square depends only on its own index, so it does not matter which thread computes which slot.
  2. Deterministic result. However the work is split, index 5 always ends up holding 25.
  3. Floating-point division. Dividing by 2.0 instead of 2 keeps the half: index 3 gives 1.5 rather than 1.
  4. Tiny arrays. With six or four elements, the parallel version brings no speed-up; it is used here only to show the result.

Example 2 — Convert Celsius Readings to Fahrenheit

Read from one array and write the converted values into another, index by index.

Java
import java.util.Arrays;

public class ArraysParallelSetAllConvert {
    public static void main(String[] args) {
        int[] celsius = {0, 25, 100, -40};
        double[] fahrenheit = new double[celsius.length];
        Arrays.parallelSetAll(fahrenheit, i -> celsius[i] * 9 / 5.0 + 32);
        System.out.println(Arrays.toString(fahrenheit));
    }
}

How It Works

  1. Same length. fahrenheit is created with celsius.length, so every index exists in both arrays.
  2. Read-only source. The generator only reads celsius[i]; nothing shared is modified, which makes it safe to run on many threads.
  3. Order of operations. celsius[i] * 9 is integer math, then / 5.0 switches to double: 25 becomes 225, then 45.0, then 77.0.
  4. A famous crossing point. -40 maps to -40.0, the temperature where both scales agree.

Example 3 — Labels for a String Array and Powers of 1024

Use the object overload for strings and the long overload for big values.

Java
import java.util.Arrays;

public class ArraysParallelSetAllLabels {
    public static void main(String[] args) {
        String[] labels = new String[5];
        Arrays.parallelSetAll(labels, i -> "item-" + (i + 1));
        System.out.println(Arrays.toString(labels));

        long[] powers = new long[5];
        Arrays.parallelSetAll(powers, i -> 1L << (i * 10));
        System.out.println(Arrays.toString(powers));
    }
}

How It Works

  1. Human-friendly numbering. "item-" + (i + 1) labels the slots from 1 to 5 while the indexes run from 0 to 4; the parentheses make the addition happen before the string join.
  2. Shifting a long. 1L << (i * 10) shifts by 0, 10, 20, 30, and 40 bits, giving powers of 1024 (210).
  3. Why 1L. Shifting the int 1 by 40 would wrap around; starting from the long 1L keeps 1099511627776 correct.
  4. No shared counter. A version that did count++ inside the lambda would be unsafe here; deriving each label from i avoids that.

Frequently Asked Questions

They produce the same result when the generator depends only on the index. parallelSetAll may split the work across several threads, which can help for very large arrays.
Yes, as long as the generator is a pure function of the index. The order of computation is not defined, but each slot gets the same value.
It runs as a parallel stream task, which uses the common ForkJoinPool by default.
No. The lambda may run on several threads at the same time, so updating shared state gives unpredictable results. Compute each value from the index instead.
For large arrays (hundreds of thousands of elements or more) or expensive per-element work. For small arrays, setAll is simpler and usually faster.

Next: Arrays.parallelSort()

Sort an array in place, using several threads for large arrays.

parallelSort() 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.

12 people found this page helpful