In Java, Arrays.parallelSort(a) sorts a into ascending order, exactly like Arrays.sort(a). For large arrays it splits the array into parts, sorts them on several threads of the common ForkJoinPool, and merges the results.
The sorted result is the same as with sort; only the speed differs. In current JDKs, arrays at or below 8192 elements (213) are simply sorted sequentially, because splitting small arrays costs more than it saves. That threshold is an implementation detail and may change.
A static method of java.util.Arrays (Java 8+): call it on the class, as in Arrays.parallelSort(...); there is no Arrays object to create. It was added in Java 8.
Syntax
Same forms as sort: whole array, range, or Comparator.
Java
int[] big = new int[1_000_000];
// ... fill big ...
Arrays.parallelSort(big);
String[] s = {"b", "c", "a"};
Arrays.parallelSort(s); // [a, b, c]
Call
What it does
Arrays.parallelSort(a)
Ascending order, whole array.
Arrays.parallelSort(a, from, to)
Sorts only indexes from to to - 1.
Arrays.parallelSort(objs, comparator)
Custom order for object arrays.
Arrays.parallelSort(objs, from, to, comparator)
Custom order within a range.
Primitive overloads exist for byte, char, short, int, long, float, and double, each with a range form. Object arrays need elements that implement Comparable, or a Comparator.
Library declarations
Java
// java.util.Arrays (since Java 8)
public static void parallelSort(int[] a)
public static void parallelSort(int[] a, int fromIndex, int toIndex)
public static <T extends Comparable<? super T>> void parallelSort(T[] a)
public static <T extends Comparable<? super T>> void parallelSort(T[] a, int fromIndex, int toIndex)
public static <T> void parallelSort(T[] a, Comparator<? super T> cmp)
public static <T> void parallelSort(T[] a, int fromIndex, int toIndex, Comparator<? super T> cmp)
// same primitive pair for byte[], char[], short[], long[], float[], double[]
Parameters
Parameter
Description
a
The array to sort. Must not be null.
fromIndex, toIndex (optional)
Range to sort: fromIndex inclusive, toIndex exclusive.
cmp (optional)
A Comparator for object arrays; null means natural order.
Length as the key.Comparator.comparing(String::length) compares 3, 4, 6, 4, and 5 instead of the letters.
Stable for ties. kiwi and plum both have four letters; kiwi came first in the input, so it stays first.
Range sort.parallelSort(mixed, 0, 3) sorts indexes 0 to 2 (9, 8, 7 become 7, 8, 9).
The rest stays put. Indexes 3 to 5 still hold 3, 2, 1 in their original order.
Frequently Asked Questions
They give the same sorted result. parallelSort can split large arrays across the common ForkJoinPool, which may be faster on multi-core machines; for small arrays it just sorts sequentially.
Usually for large arrays (many thousands of elements or more) on a machine with several cores. Measure with your own data, because the benefit depends on the hardware and workload.
The object versions are documented as stable, so equal elements keep their order. For primitives, stability does not matter.
No. There is no boolean[] overload, just as with Arrays.sort.