Java Arrays parallelSort(T[] a) Method



Description

The Java Arrays parallelSort(T[] a) method sorts the specified array of objects into ascending numerical order. This method using a parallel sort-merge algorithm which breaks the array into subarrays, sorted them and then merged to give a sorted array.

Declaration

Following is the declaration for java.util.Arrays.parallelSort(T[]) method

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

Type Parameters

T − This is the class of the objects to be sorted.

Parameters

a − This is the array to be sorted.

Return Value

This method does not return any value.

Exception

  • ClassCastException − if the array contains elements that are not mutually comparable (for example, strings and integers).

  • IllegalArgumentException − if the natural ordering of the array elements is found to violate the Comparable contract.

Java Arrays parallelSort​(T[] array, int fromIndex, int toIndex) Method

Description

The Java Arrays parallelSort(T[], int fromIndex, int toIndex) method sorts the specified range of given array of objects into ascending numerical order. This method using a parallel sort-merge algorithm which breaks the array into subarrays, sorted them and then merged to give a sorted array.

Declaration

Following is the declaration for java.util.Arrays.parallelSort(T[], int fromIndex, int toIndex) method

public static <T extends Comparable<? super T>> void parallelSort​(T[] a, int fromIndex, int toIndex)

Type Parameters

T − This is the class of the objects to be sorted.

Parameters

  • array − This is the array to be sorted.

  • fromIndex − This is the index of the first element, inclusive, to be sorted.

  • toIndex − This is the index of the last element, exclusive, to be sorted

Return Value

This method is not returning anything.

Exception

  • IllegalArgumentException − if fromIndex > toIndex or if the natural ordering of the array elements is found to violate the Comparable contract.

  • ArrayIndexOutOfBoundsException − if fromIndex < 0 or toIndex > array.length

  • ClassCastException − if the array contains elements that are not mutually comparable (for example, strings and integers).

Sorting an Array of Objects using a Custom Comparator Example

The following example shows the usage of Java Arrays parallelSort(T[]) method. First, we've created an array of objects, printed the original array. Using parallelSort() method, array is sorted and printed thereafter.

package com.tutorialspoint;

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {
      // initialize unsorted array
      Student arr[] = { new Student(1, "Julie"), new Student(3, "Adam"), new Student(2, "Robert") };

      System.out.print("Original Array: [");
      // print the array
      for (int i = 0; i < arr.length; i++) {
         System.out.print(arr[i] + " ");
      }
      System.out.println("]");
     
      // sort the array
      Arrays.parallelSort(arr);

      System.out.print("Sorted Array: [");
      // print the array
      for (int i = 0; i < arr.length; i++) {
         System.out.print(arr[i] + " ");
      }
      System.out.println("]");
   }
}
class Student implements Comparable<Student> {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }

   @Override
   public int compareTo(Student student) {
      return this.rollNo - student.rollNo;
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Original Array: [[ 1, Julie ] [ 3, Adam ] [ 2, Robert ] ]
Sorted Array: [[ 1, Julie ] [ 2, Robert ] [ 3, Adam ] ]

Sorting an Array of Objects with a Range using a Custom Comparator Example

The following example shows the usage of Java Arrays parallelSort(T[], int, int) method. First, we've created an array of objects, printed the original array. Using parallelSort() method, array is sorted and printed thereafter.

package com.tutorialspoint;

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {
      // initialize unsorted array
      Student arr[] = { new Student(1, "Julie"), new Student(3, "Adam"), new Student(2, "Robert") };

      System.out.print("Original Array: [");
      // print the array
      for (int i = 0; i < arr.length; i++) {
         System.out.print(arr[i] + " ");
      }
      System.out.println("]");
     
      // sort the array
      Arrays.parallelSort(arr, 0, arr.length);

      System.out.print("Sorted Array: [");
      // print the array
      for (int i = 0; i < arr.length; i++) {
         System.out.print(arr[i] + " ");
      }
      System.out.println("]");
   }
}
class Student implements Comparable<Student> {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }

   @Override
   public int compareTo(Student student) {
      return this.rollNo - student.rollNo;
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Original Array: [[ 1, Julie ] [ 3, Adam ] [ 2, Robert ] ]
Sorted Array: [[ 1, Julie ] [ 2, Robert ] [ 3, Adam ] ]

Sorting a Sub-Array of Objects using a Custom Comparator Example

The following example shows the usage of Java Arrays parallelSort(T[], int, int) method. First, we've created an array of objects, printed the original array. Using parallelSort() method, a sub-array is sorted and printed thereafter.

package com.tutorialspoint;

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {
      // initialize unsorted array
      Student arr[] = { new Student(3, "Adam"), new Student(1, "Julie"), new Student(2, "Robert") };

      System.out.print("Original Array: [");
      // print the array
      for (int i = 0; i < arr.length; i++) {
         System.out.print(arr[i] + " ");
      }
      System.out.println("]");
     
      // sort first two elements of the array 
      Arrays.parallelSort(arr, 0, 2);

      System.out.print("Sorted Array: [");
      // print the array
      for (int i = 0; i < arr.length; i++) {
         System.out.print(arr[i] + " ");
      }
      System.out.println("]");
   }
}
class Student implements Comparable<Student> {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }

   @Override
   public int compareTo(Student student) {
      return this.rollNo - student.rollNo;
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Original Array: [[ 3, Adam ] [ 1, Julie ] [ 2, Robert ] ]
Sorted Array: [[ 1, Julie ] [ 3, Adam ] [ 2, Robert ] ]
java_util_arrays.htm
Advertisements