有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

数组如何使用Java 8的parallelSort()对映射列表进行排序

我知道如何使用ComparatorCollections.sort()对任何类型的对象进行排序,但我想知道如何使用Arrays.parallelSort()对地图列表进行排序?因为它只能对普通数组进行排序

这是我使用Comparator对其进行排序的代码

List<Map<String, Integer>> employees = new ArrayList<Map<String, Integer>>() {{
    add(new HashMap<String, Integer>() {{put("position",5); put("id2", 9);}});
    add(new HashMap<String, Integer>() {{put("position",1); put("id2", 1);}});
    add(new HashMap<String, Integer>() {{put("position",2); put("id2", 2);}});
    add(new HashMap<String, Integer>() {{put("position",4); put("id2", 5);}});
    add(new HashMap<String, Integer>() {{put("position",1); put("id2", 1);}});
    add(new HashMap<String, Integer>() {{put("position",4); put("id2", 7);}});
}};

Comparator<Map<String, Integer>> comparator = new Comparator<Map<String, Integer>>() {
        @Override
        public int compare(Map<String, Integer> o1,Map<String, Integer> o2) {
                int nr1 = o1.get("id2");
                int nr2 = o2.get("id2");
                return Integer.compare(nr2, nr1);
        }
};

Collections.sort(employees,comparator);

for (Map<String, Integer> s : employees){
    System.out.println(s);
}

Arrays.parallelSort确实有一个名为parallelSort(T[] a,Comparator<?super T> c)的方法,但我不知道如何正确使用它

我已经试过了

Arrays.parallelSort(new ArrayList<Map<String, Integer>>(employees.size()), comparator);

当然我会犯这个错误

The method parallelSort(T[], Comparator<? super T>) in the type Arrays is not applicable for the arguments (ArrayList<Map<String,Integer>>, Comparator<Map<String,Integer>>)

我只是好奇,这种类型的数据是否可以使用parallelSort进行排序

附言:我也知道如何使用Java8stream().sorted进行排序,但我不想使用它

编辑:我正在按降序对id2进行排序


共 (0) 个答案