Java Utility Library

Java Arrays - parallelSort() Method



The java.util.Arrays.parallelSort() method is used to sort the specified array of objects into ascending order, according to the natural ordering of its elements. All elements in the array must implement the Comparable interface. Furthermore, all elements in the array must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the array).

This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort.

Syntax

public static <T extends Comparable<? super T>> void parallelSort(T[] a)

Here, T is the type of elements in the array.


Parameters

a Specify the array to be sorted.

Return Value

void type.

Exception

  • Throws ClassCastException, if the array contains elements that are not mutually comparable (for example, strings and integers).
  • Throws IllegalArgumentException, (optional) if the natural ordering of the array elements is found to violate the Comparable contract.

Example:

In the example below, the java.util.Arrays.parallelSort() method is used to sort a specified array of objects.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating an unsorted Integer array
    Integer Arr[] = {10, 2, -3, 35, 56};

    //printing array before sorting
    System.out.print("Arr contains:"); 
    for(Integer i: Arr)
      System.out.print(" " + i);

    //sort the array
    Arrays.sort(Arr);

    //printing array after sorting
    System.out.print("\nArr contains:"); 
    for(Integer i: Arr)
      System.out.print(" " + i);   
  }
}

The output of the above code will be:

Arr contains: 10 2 -3 35 56
Arr contains: -3 2 10 35 56

❮ Java.util - Arrays