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]
Call
What 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
Parameter
Description
array
The array to fill. Must not be null.
generator
A side-effect-free function from index to value. Must not be null.
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));
}
}
Output
[0, 1, 4, 9, 16, 25]
[0.0, 0.5, 1.0, 1.5]
How It Works
Independent elements. Each square depends only on its own index, so it does not matter which thread computes which slot.
Deterministic result. However the work is split, index 5 always ends up holding 25.
Floating-point division. Dividing by 2.0 instead of 2 keeps the half: index 3 gives 1.5 rather than 1.
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));
}
}
Output
[32.0, 77.0, 212.0, -40.0]
How It Works
Same length.fahrenheit is created with celsius.length, so every index exists in both arrays.
Read-only source. The generator only reads celsius[i]; nothing shared is modified, which makes it safe to run on many threads.
Order of operations.celsius[i] * 9 is integer math, then / 5.0 switches to double: 25 becomes 225, then 45.0, then 77.0.
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));
}
}
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.
Shifting a long.1L << (i * 10) shifts by 0, 10, 20, 30, and 40 bits, giving powers of 1024 (210).
Why 1L. Shifting the int 1 by 40 would wrap around; starting from the long 1L keeps 1099511627776 correct.
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.