Java Arrays.parallelSort() Method

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

Definition

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

ParameterDescription
aThe 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.

Return Value

Return ValueDescription
voidNothing. The array is sorted in place.

Compare with Similar Methods

Sorting and parallel relatives:

MethodHow it differs
Arrays.sort(a)Always single-threaded; best for small and medium arrays.
Arrays.parallelSetAll(a, f)Fills an array in parallel.
Arrays.parallelPrefix(a, op)Running totals in parallel.
Arrays.binarySearch(a, key)Search the array once it is sorted.

Java Examples

Example 1 — parallelSort Works Like sort

Sort a few numbers and city names; the call looks exactly like Arrays.sort.

Java
import java.util.Arrays;

public class ArraysParallelSortDemo {
    public static void main(String[] args) {
        int[] nums = {42, 7, 19, 3, 88, 21};
        Arrays.parallelSort(nums);
        System.out.println(Arrays.toString(nums));

        String[] cities = {"Oslo", "Lima", "Cairo", "Bern"};
        Arrays.parallelSort(cities);
        System.out.println(Arrays.toString(cities));
    }
}

How It Works

  1. Drop-in replacement. Swapping sort for parallelSort needs no other code change.
  2. Small arrays run sequentially. Six numbers are far below the splitting threshold, so in current JDKs this is an ordinary single-threaded sort.
  3. Strings by natural order. String implements Comparable, so the cities sort alphabetically: Bern, Cairo, Lima, Oslo.
  4. In place. Both arrays are rearranged directly; the method returns nothing.

Example 2 — Sort 100,000 Numbers and Check Against sort()

Build a large shuffled array with a formula, sort one copy each way, and compare.

Java
import java.util.Arrays;

public class ArraysParallelSortLarge {
    public static void main(String[] args) {
        int n = 100_000;
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = (int) ((i * 7919L) % n);
        }
        int[] b = a.clone();
        System.out.println("before: " + Arrays.toString(Arrays.copyOf(a, 3)));

        Arrays.parallelSort(a);
        Arrays.sort(b);
        System.out.println("same result: " + Arrays.equals(a, b));
        System.out.println("first three: " + Arrays.toString(Arrays.copyOf(a, 3)));
        System.out.println("last: " + a[n - 1]);
    }
}

How It Works

  1. A shuffle without Random. 7919 shares no factor with 100000, so (i * 7919) % n visits every number from 0 to 99999 exactly once, in a scrambled order.
  2. Big enough to split. 100,000 elements is well above the sequential threshold, so the JDK can spread the work over several threads.
  3. Identical results. Arrays.equals(a, b) confirms that the parallel and sequential sorts produced the same order.
  4. Known answer. Because the values are a permutation of 0 to 99999, the sorted array starts 0, 1, 2 and ends with 99999.

Example 3 — Sort by Length, and Sort a Range

Order words by length with a comparator, then sort only the first half of an array.

Java
import java.util.Arrays;
import java.util.Comparator;

public class ArraysParallelSortComparator {
    public static void main(String[] args) {
        String[] words = {"kiwi", "fig", "banana", "plum", "apple"};
        Arrays.parallelSort(words, Comparator.comparing(String::length));
        System.out.println(Arrays.toString(words));

        int[] mixed = {9, 8, 7, 3, 2, 1};
        Arrays.parallelSort(mixed, 0, 3);
        System.out.println(Arrays.toString(mixed));
    }
}

How It Works

  1. Length as the key. Comparator.comparing(String::length) compares 3, 4, 6, 4, and 5 instead of the letters.
  2. Stable for ties. kiwi and plum both have four letters; kiwi came first in the input, so it stays first.
  3. Range sort. parallelSort(mixed, 0, 3) sorts indexes 0 to 2 (9, 8, 7 become 7, 8, 9).
  4. 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.
Java 8.

Next: Arrays.setAll()

Compute every element from its index with a function.

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

13 people found this page helpful