按降序对 Map 中的值列表进行排序

Sort List of Values in a Map in descending order

我有一张包含双打列表的地图。

未排序的地图内容:{A=[0.02, 0.03], B=[0.0, 0.01], C=[0.01, 0.0], D=[0.05, 1.03], E=[1.01, 0.03]}

我需要对 List(0) 进行降序排序。我已经尝试了以下逻辑,但它是按升序排序的。

Map<String, List<Double>> map = new HashMap<>();

map.put("A", new ArrayList<Double>(Arrays.asList(0.02,0.03)));
map.put("B", new ArrayList<Double>(Arrays.asList(0.00,0.01)));
map.put("C", new ArrayList<Double>(Arrays.asList(0.01,0.00)));
map.put("D", new ArrayList<Double>(Arrays.asList(0.05,1.03)));
map.put("E", new ArrayList<Double>(Arrays.asList(1.01,0.03)));
        
        
Map<String, List<Double>> output = map.entrySet()
                .stream()
                .sorted(Comparator.comparing(o -> o.getValue().get(0)))
                .collect(Collectors
                        .toMap(Map.Entry::getKey,Map.Entry::getValue,(a,b) ->a, LinkedHashMap :: new));
        
        
System.out.println("After sort: " + output);

实际输出: {B=[0.0, 0.01], C=[0.01, 0.0], A=[0.02, 0.03], D=[0.05, 1.03], E=[1.01, 0.03]}

预期输出: {E=[1.01, 0.03], D=[0.05, 1.03], A=[0.02, 0.03], C=[0.01, 0.0], B=[0.0, 0.1]}

请帮助我了解 Java8 流的逻辑。

试试这个:

Map<String, List<Double>> output = map.entrySet()
            .stream()
            .sorted(Collections.reverseOrder(Comparator.comparing(o -> o.getValue().get(0))))
            .collect(Collectors
                    .toMap(Map.Entry::getKey,Map.Entry::getValue,(a,b) ->a, LinkedHashMap :: new));