使用并行排序和多个字段对列表进行排序
Sort a list using parallel sorting and multiple fields
我有一个对象列表,我正在根据两个参数对这个列表进行排序:
1. 名字,和
2. 姓氏。
所以要求就像,首先根据名字对列表进行排序,然后根据姓氏进行排序。
我已经使用 Comparator.compairing 和 thenCompairing 方法实现了这个,如下所示:
Comparator<Employee> groupComparator = Comparator.comparing(Employee::getFirstName)
.thenComparing(Employee::getLastName);
而且,它工作得很好。
现在我想要的是使用并行的概念 processing/sorting 在多线程或多线程环境中并行排序。知道如何实现吗?
我使用Arrays.parallelSort方法得到的解决方案如下:
List<Employee> emps = getEmployees();
Comparator<Employee> groupComparator = Comparator.comparing(Employee::getFirstName)
.thenComparing(Employee::getLastName);
Employee[] empArr = employees.toArray(new Employee[emps.size()]);
//Parallel sorting
Arrays.parallelSort(empArr, groupComparator);
已添加 java 8 并根据文档:
The sorting algorithm is a parallel sort-merge that breaks the array into sub-arrays that are themselves sorted and then merged. When the sub-array length reaches a minimum granularity, the sub-array is sorted using the appropriate Arrays.sort method. If the length of the specified array is less than the minimum granularity, then it is sorted using the appropriate Arrays.sort method. The algorithm requires a working space no greater than the size of the original array. The ForkJoin common pool is used to execute any parallel tasks.
它将解决我这里的问题
这也可以通过使用 streams
来实现。通过使用 streams
我们不需要在排序前将 List
复制到数组。
Stream<Employee> sorted = employees.stream()
.sorted(Comparator.comparing(Employee::getFirstName)
.thenComparing(Employee::getLastName))
.parallel();
sorted.forEachOrdered(System.out::println);
稍微修改一下,但这应该也可以:
List<Employee> sortedEmployees = employees.parallelStream()
.sorted(Comparator.comparing(Employee::getFirstName)
.thenComparing(Employee::getLastName))
.collect(Collectors.toList());
你看 - 这是流的优点之一 - 将解决方案转化为并行可能就像将 stream()
转化为 parallelStream()
一样简单!
但是像往常一样使用 parallelStream:
- 衡量其影响
- 为意外和微调的需要做好准备
我有一个对象列表,我正在根据两个参数对这个列表进行排序: 1. 名字,和 2. 姓氏。
所以要求就像,首先根据名字对列表进行排序,然后根据姓氏进行排序。
我已经使用 Comparator.compairing 和 thenCompairing 方法实现了这个,如下所示:
Comparator<Employee> groupComparator = Comparator.comparing(Employee::getFirstName)
.thenComparing(Employee::getLastName);
而且,它工作得很好。
现在我想要的是使用并行的概念 processing/sorting 在多线程或多线程环境中并行排序。知道如何实现吗?
我使用Arrays.parallelSort方法得到的解决方案如下:
List<Employee> emps = getEmployees();
Comparator<Employee> groupComparator = Comparator.comparing(Employee::getFirstName)
.thenComparing(Employee::getLastName);
Employee[] empArr = employees.toArray(new Employee[emps.size()]);
//Parallel sorting
Arrays.parallelSort(empArr, groupComparator);
已添加 java 8 并根据文档:
The sorting algorithm is a parallel sort-merge that breaks the array into sub-arrays that are themselves sorted and then merged. When the sub-array length reaches a minimum granularity, the sub-array is sorted using the appropriate Arrays.sort method. If the length of the specified array is less than the minimum granularity, then it is sorted using the appropriate Arrays.sort method. The algorithm requires a working space no greater than the size of the original array. The ForkJoin common pool is used to execute any parallel tasks.
它将解决我这里的问题
这也可以通过使用 streams
来实现。通过使用 streams
我们不需要在排序前将 List
复制到数组。
Stream<Employee> sorted = employees.stream()
.sorted(Comparator.comparing(Employee::getFirstName)
.thenComparing(Employee::getLastName))
.parallel();
sorted.forEachOrdered(System.out::println);
稍微修改一下,但这应该也可以:
List<Employee> sortedEmployees = employees.parallelStream()
.sorted(Comparator.comparing(Employee::getFirstName)
.thenComparing(Employee::getLastName))
.collect(Collectors.toList());
你看 - 这是流的优点之一 - 将解决方案转化为并行可能就像将 stream()
转化为 parallelStream()
一样简单!
但是像往常一样使用 parallelStream:
- 衡量其影响
- 为意外和微调的需要做好准备