Fast array sorting with java 8. Thanks to its new functions you can now sort multithreaded with one line of code. Simple and Fast.
Today we’re gonna take a look at one of Java8‘s function for fast array sorting:
Arrays.parallelSort(). It’s kinda the same as
Arrays.sort(), but differently.
What’s the difference?
It’s faster. Well, not every time. But for Arrays with more than 2000 Elements, it’s faster. Why? Due to the Fork/Join framework introduced in Java7. The function assigns the sorting tasks to multiple threads available in the thread pool. This makes it faster in comparison to
sort(). The efficiency grows directly proportional to the number of elements in the array.
Here’s how it affects the speed: (x-axis is time, y-axis is number of elements)
The function is pretty simple to use:
Arrays.parralelSort(myArray, [optional int fromIndex], [optional int toIndex]);