Home  >  Java 8

Java 8 Arrays Parallel Sort Example

By Arvind Rai, August 22, 2016
On this page we will provide java 8 Arrays parallel sort example. Java 8 has introduced a new method parallelSort() in Arrays class. Find the detail point wise.
1. : Java 8 Arrays.parallelSort() uses parallel sort-merge algorithm which breaks the array into sub arrays and then they themselves are sorted and merged.
2. : Arrays are broken into sub arrays and sub arrays are again broken into another sub arrays and continue till the length of sub arrays reaches to a minimum granularity.
3. : After many partition once the sub arrays reaches to minimum granularity then they are sorted by using Arrays.sort().
4. : Arrays.sort() uses Dual-Pivot Quicksort algorithm to sort the elements.
5. : All the parallel task used by Arrays.parallelSort() is performed by ForkJoin common pool.
6. : Arrays.parallelSort() sorts the complete arrays or the elements between the given to and from index.
7. : If the size of array is less than minimum granularity, then there is no parallel processing.

Arrays.parallelSort() Method Description

Find the method description of Arrays.parallelSort()
1. : Find the method which sorts the complete array in natural order. The objects must be Comparable.
void parallelSort(T[] a)
 

2. : It sorts the elements between from and to index. The objects must be Comparable.
void parallelSort(T[] a, int fromIndex, int toIndex)
 

3. : It sorts the complete array. Sorting is performed on the basis of given Comparator object.
void parallelSort(T[] a, Comparator<? super T> cmp)
 

4. : It sorts the elements between from and to index. Sorting is performed on the basis of given Comparator object.
void parallelSort(T[] a, int fromIndex, int toIndex, Comparator<? super T> cmp)
 

5. : Arrays.parallelSort() is also used with primitive data type.

Arrays.parallelSort() with Comparable


ParallelSortWithComparable.java
package com.concretepage;
import java.util.Arrays;
import java.util.function.Consumer;
public class ParallelSortWithComparable {
	public static void main(String[] args) {
		User[] users = User.getUsers();
		System.out.println("--Sort complete array--");
		Arrays.parallelSort(users);
		Consumer<User> printUser = u-> System.out.println(u.getName()+"-"+u.getAge());
		Arrays.stream(users).forEach(printUser);		
		System.out.println("--Sort array from index 1 to 4--");
		users = User.getUsers();
		Arrays.parallelSort(users, 1, 4);
		Arrays.stream(users).forEach(printUser);
	}
} 
User.java
package com.concretepage;
public class User implements Comparable<User> {
	private String name;
	private int age;	
	public User(String name, int age) {
		this.name = name;
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public int getAge() {
		return age;
	}
	@Override
	public int compareTo(User user) {
		return name.compareTo(user.name);
	}
	public static User[] getUsers() {
		User[] users = new User[6];
		users[0] = new User ("Ram", 25);
		users[1] = new User ("Shyam", 22);
		users[2] = new User ("Mohan", 21);
		users[3] = new User ("Suresh", 30);
		users[4] = new User ("Ramesh", 20);
		users[5] = new User ("Dinesh", 27);
		return users;
	}
} 
Output
--Sort complete array--
Dinesh-27
Mohan-21
Ram-25
Ramesh-20
Shyam-22
Suresh-30
--Sort array from index 1 to 4--
Ram-25
Mohan-21
Shyam-22
Suresh-30
Ramesh-20
Dinesh-27 

Arrays.parallelSort() with Comparator


ParallelSortWithComparator.java
package com.concretepage;
import java.util.Arrays;
import java.util.Comparator;
import java.util.function.Consumer;
public class ParallelSortWithComparator {
	public static void main(String[] args) {
		User[] users = User.getUsers();
		Comparator<User> ageComparator = Comparator.comparing(User::getAge);
		System.out.println("--Sort complete array--");
		Arrays.parallelSort(users, ageComparator);
		Consumer<User> printUser = u-> System.out.println(u.getName()+"-"+u.getAge());
		Arrays.stream(users).forEach(printUser);
		System.out.println("--Sort array from index 1 to 4--");
		users = User.getUsers();
		Arrays.parallelSort(users, 1, 4, ageComparator);
		Arrays.stream(users).forEach(printUser);
	}
} 
Output
--Sort complete array--
Ramesh-20
Mohan-21
Shyam-22
Ram-25
Dinesh-27
Suresh-30
--Sort array from index 1 to 4--
Ram-25
Mohan-21
Shyam-22
Suresh-30
Ramesh-20
Dinesh-27 

Arrays.parallelSort() with Primitive Data Type


ParallelSortWithPrimitiveDataType.java
package com.concretepage;
import java.util.Arrays;
import java.util.function.DoubleConsumer;
import java.util.function.IntConsumer;
public class ParallelSortWithPrimitiveDataType {
	public static void main(String[] args) {
		int[] num1 = {3, 6, 2, 10, 4, 1, 7};
		System.out.println("--Sort complete Integer array--");
		Arrays.parallelSort(num1);
		IntConsumer  printInt = i -> System.out.print(i+" ");
		Arrays.stream(num1).forEach(printInt);
		System.out.println("\n--Sort Integer array from index 1 to 5--");
		int[] num2 = {3, 6, 2, 10, 4, 1, 7};
		Arrays.parallelSort(num2, 1, 5);
		Arrays.stream(num1).forEach(printInt);		
		
		double[] db1 = {3.5, 1.2, 6.7, 8.9, 0.6, 2.3, 5.5};
		System.out.println("\n--Sort complete Double array--");
		Arrays.parallelSort(db1);
		DoubleConsumer  printDB = d -> System.out.print(d+" ");
		Arrays.stream(db1).forEach(printDB);
		System.out.println("\n--Sort Double array from index 1 to 5--");
		double[] db2 = {3.5, 1.2, 6.7, 8.9, 0.6, 2.3, 5.5};
		Arrays.parallelSort(db2, 1, 5);
		Arrays.stream(db2).forEach(printDB);	
	}
} 
Output
--Sort complete Integer array--
1 2 3 4 6 7 10 
--Sort Integer array from index 1 to 5--
1 2 3 4 6 7 10 
--Sort complete Double array--
0.6 1.2 2.3 3.5 5.5 6.7 8.9 
--Sort Double array from index 1 to 5--
3.5 0.6 1.2 6.7 8.9 2.3 5.5  

Reference

Java Doc: Class Arrays
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2018 concretepage.com | Privacy Policy | Contact Us