- Java.util Package Classes
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util Package Extras
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
Java Arrays parallelSort(T[] a, Comparator<? super T> cmp) Method
Description
The Java Arrays parallelSort(T[]) method sorts the specified array of objects into ascending numerical order using the order sepcificed by the Comparator. 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[] ,Comparator<? super T> cmp) method
public static <T extends Comparable<? super T>> void parallelSort(T[] a,Comparator<? super T> cmp)
Parameters
a − This is the array to be sorted.
cmp − This is the comparator to determine the order of the array. A null value indicates that the elements' natural ordering should be used.
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, Comparator<? super T> cmp) Method
Description
The Java Arrays parallelSort(T[], int fromIndex, int toIndex,Comparator<? super T> cmp) method sorts the specified range of given array of objects using the order sepcificed by the Comparator. 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[] a, int fromIndex, int toIndex,Comparator<? super T> cmp) method
public static <T extends Comparable<? super T>> void parallelSort​(T[] a, int fromIndex, int toIndex,Comparator<? super T> cmp)
Parameters
a − 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
cmp − This is the comparator to determine the order of the array. A null value indicates that the elements' natural ordering should be used.
Return Value
This method is not returning anything.
Exception
IllegalArgumentException − if fromIndex > toIndex
ArrayIndexOutOfBoundsException − if fromIndex < 0 or toIndex > array.length
Sorting an Array of Object Example
The following example shows the usage of Java Arrays parallelSort(T[], Comparator) 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; import java.util.Comparator; 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("]"); RollNoComparator comparator = new RollNoComparator(); // sort the array Arrays.parallelSort(arr, comparator); 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 { 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); } public int getRollNo() { return rollNo; } public void setRollNo(int rollNo) { this.rollNo = rollNo; } public String getName() { return name; } public void setName(String name) { this.name = name; } } class RollNoComparator implements Comparator<Student>{ @Override public int compare(Student o1, Student o2) { return o1.getRollNo()-o2.getRollNo(); } }
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, Comparator) 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; import java.util.Comparator; 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("]"); RollNoComparator comparator = new RollNoComparator(); // sort the array Arrays.parallelSort(arr, 0, arr.length, comparator); 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 { 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); } public int getRollNo() { return rollNo; } public void setRollNo(int rollNo) { this.rollNo = rollNo; } public String getName() { return name; } public void setName(String name) { this.name = name; } } class RollNoComparator implements Comparator<Student>{ @Override public int compare(Student o1, Student o2) { return o1.getRollNo()-o2.getRollNo(); } }
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 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; import java.util.Comparator; 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("]"); RollNoComparator comparator = new RollNoComparator(); // sort first two elements of the array Arrays.parallelSort(arr, 0, 2, comparator); 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 { 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); } public int getRollNo() { return rollNo; } public void setRollNo(int rollNo) { this.rollNo = rollNo; } public String getName() { return name; } public void setName(String name) { this.name = name; } } class RollNoComparator implements Comparator<Student>{ @Override public int compare(Student o1, Student o2) { return o1.getRollNo()-o2.getRollNo(); } }
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 ] ]
To Continue Learning Please Login
Login with Google