Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Java 8 have Arrays.parallelSort() but not Collections.parallelSort()?

Java 8 introduces a parallel algorithm for multi-threaded sorting of arrays, in the form of the overloaded Arrays.sort() methods.

Why does it not also provide a Collections.parallelSort(), for multi-threaded sorting of a List?

like image 654
chiastic-security Avatar asked Dec 23 '14 18:12

chiastic-security


People also ask

What is the use of the parallelSort () method?

parallelSort() method uses concept of MultiThreading which makes the sorting faster as compared to normal sorting method. Note : Different time intervals will be printed But parallel sort will be done before normal sort.

What is parallelSort?

Parallel Sort uses Fork/Join framework introduced in Java 7 to assign the sorting tasks to multiple threads available in the thread pool. Fork/Join implements a work stealing algorithm where in a idle thread can steal tasks queued up in another thread.

What is parallel array sorting in Java?

Java Parallel Array Sorting. Java provides a new additional feature in Array class which is used to sort array elements parallel. New methods has added to java. util. Arrays package that use the JSR 166 Fork/Join parallelism common pool to provide sorting of arrays in parallel.

How the collection objects are sorted in Java?

Collections class provides static methods for sorting the elements of a collection. If collection elements are of a Set type, we can use TreeSet. However, we cannot sort the elements of List. Collections class provides methods for sorting the elements of List type elements.


1 Answers

A List does not necessarily allow efficient implementation of the same parallel sorting algorithms that an array does. You may be able to directly apply it to an ArrayList, but most likely not to a LinkedList, due to its lack of efficient random access. There are efficient multi-threaded sorting algorithms for that kind of list, but they are different from a random-access list.

And, in fact, the thread-safe implementation of the List interface may not support efficient external multi-threaded sorting at all, due to synchronization. Providing a generic sorting algorithm for those would be impossible, and in fact a parallel algorithm might be slower on them than a sequential algorithm.

like image 183
Wormbo Avatar answered Oct 22 '22 04:10

Wormbo