Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Friday 6 October 2017

Java 8 Features - Parallel Array Sorting with Examples

Parallel Array Sorting in Java

Java 8 Features - Parallel Sorting Array

Now, Here we are going to discuss the most interesting feature of java 8 which is parallel array sorting. In the last post we saw what is the use of default and static method in an interface of java 8 version.

Let's start parallel arrays sorting.

Java 8 provides a new feature which is parallel array sort by using this feature we can sort an array elements parallel.

Arrays.sort() method is also used to sort and array element but java 8 provides a new method which is parallelSort() method for sorting an array elements parallel.

The parallelSort() method is belong to the Arrays class which is available in java.util package.

The parallelSort() method is faster than Array.sort() method and this method follows fork/join framework to assign the sorting tasks to multiple threads that are available in the thread pool.

The parallelSort() method are overloaded for all primitive data-types and comparable objects.

There are many methods in arrays class but we will see some overloaded sorting methods of arrays class.


Parallel Sorting Overloaded Methods

(1) public static void parallelSort(byte[] a)

This method sorts the specified array into ascending numerical order.

(2) public static void parallelSort(byte[] a, int fromIndex, int toIndex)

This method sorts the specified range of the array into ascending numerical order.

(3) public static void parallelSort(char[] a)

This method sorts the specified array into ascending numerical order.

(4) public static void parallelSort(char[] a, int fromIndex, int toIndex)

This method sorts the specified range of the array into ascending numerical order.

(5) public static void parallelSort(double[] a)

This method sorts the specified array into ascending numerical order.

(6) public static void parallelSort(double[] a, int fromIndex, int toIndex)

This method sorts the specified range of the array into ascending numerical order.

(7) public static void parallelSort(float[] a)

This method sorts the specified array into ascending numerical order.

(8) public static void parallelSort(float[] a, int fromIndex, int toIndex)

This method sorts the specified range of the array into ascending numerical order.

(9) public static void parallelSort(int[] a)

This method sorts the specified array into ascending numerical order.

(10) public static void parallelSort(int[] a, int fromIndex, int toIndex)

This method sorts the specified range of the array into ascending numerical order.

(11) public static void parallelSort(long[] a)

This method sorts the specified array into ascending numerical order.

(12) public static void parallelSort(long[] a, int fromIndex, int toIndex)

This method sorts the specified range of array into ascending numerical order.

(13) public static void parallelSort(short[] a)

This method sorts the specified array into ascending numerical order.

(14) public static void parallelSort(sort[] a, int fromIndex, int toIndex)

This method sorts the specified range of array into ascending numerical order.

And there are other parallel sort methods in arrays class.

Let's understand with it example


Java 8 Example - Parallel Array Sort Java 

This is the simple example where we use Arrays.parallelSort() method for sorting the array elements.

import java.util.Arrays;
class ParallelSortExample
{
public static void main(String args[])
{
//creating integer array elements
int a[] = {50, 10, 90, 40, 80};
//Now, using Arrays.parallelSort() method for sorting array
Arrays.parallelSort(a);
for(int i : a)
{
System.out.println(i);
}
}
}

Output: 10
             40
             50
             80
             90

Now, here we have discussed new features of java 8 which is parallel array sorting.

Share:

0 comments:

Post a Comment

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate