根据 Java 中数组列表的值对 Map 进行降序排序

Sort a Map in descending order based on value of arraylist in Java

我有一个地图类型

static Map<String, ArrayList<String>> Container = new HashMap<String, ArrayList<String>>();

值数组列表有两个元素,第一个索引是扩展,第二个是大小,

Container : {
C:\HTML\viewer\files\output\ViewerJS\commons-io-2.11.0-src (2).zip=[zip, 919362], 
C:\HTML\viewer\files\jar_files (2).zip=[zip, 345068], 
C:\HTML\viewer\files\output\ViewerJS\rar1.rar=[rar, 1], 
C:\HTML\viewer\files\output\files.zip=[zip, 10184010], 
C:\HTML\viewer\files\check2.zip=[zip, 1]
}

我希望根据地图值索引 1 中的数字降序对地图进行排序,地图值的大小也是 STRING.

不胜感激,谢谢。

TreeMap可以按键排序LinkedHashMap保持插入顺序

很可能,您只需要按所需顺序对地图内容的输出进行排序,这可以使用简单的 Comparator.comparingComparator.reverseOrder():

来实现
Container.entrySet()
    .stream()
    .sorted(Comparator.comparing(
        e -> Long.parseLong(e.getValue().get(1)), Comparator.reverseOrder()
    ))
    .forEach(System.out::println);

输出:

C:\HTML\viewer\files\output\files.zip=[zip, 10184010]
C:\HTML\viewer\files\output\ViewerJS\commons-io-2.11.0-src (2).zip=[zip, 919362]
C:\HTML\viewer\files\jar_files (2).zip=[zip, 345068]
C:\HTML\viewer\files\check2.zip=[zip, 1]
C:\HTML\viewer\files\output\ViewerJS\rar1.rar=[rar, 1]

或者排序的结果可能会re-collected变成LinkedHashMap(使用Map.Entry.comparingByValue的例子):

Container = Container.entrySet()
    .stream()
    .sorted(Map.Entry.comparingByValue(Comparator.comparing(
        arr -> Long.parseLong(arr.get(1)), Comparator.reverseOrder()
    )))
    .collect(Collectors.toMap(
        Map.Entry::getKey,
        Map.Entry::getValue,
        (a, b) -> a,
        LinkedHashMap::new
    ));

这使用 TreeMap 和实用函数 (addElementsToMap) 来更新您的地图。

注意:比较器在地图创建时提供。

public class DriverClass {


private static Map<Integer, ArrayList<String>> container = new TreeMap<>((o1, o2) -> (o2 < o1) ? -1 : 1);

public static void main(String[] args) {
    DriverClass driver = new DriverClass();
    String[] elementsList = driver.collectElements();
    driver.addElementsToMap(elementsList);
    container.entrySet().forEach(e -> System.out.println(e.getKey() + " ----- " + e.getValue()));

}

private void addElementsToMap(String[] elementsList) {
     int i = 0;
    for (int j = elementsList.length/2; j <= elementsList.length-1; j++) {
       int size = Integer.parseInt(elementsList[i+1].replaceAll("]", ""));
        container.put(size, new ArrayList<>(Arrays.asList(elementsList[i], elementsList[i+1])));
        i += 2;
    }
}

private String[] collectElements() {
    return new String[] {
        "C:\HTML\viewer\files\output\ViewerJS\commons-io-2.11.0-src (2).zip=[zip", "919362]",
        "C:\HTML\viewer\files\jar_files (2).zip=[zip", "345068]",
        " C:\HTML\viewer\files\output\ViewerJS\rar1.rar=[rar", "1]",
        "C:\HTML\viewer\files\output\files.zip=[zip", "10184010]",
        "C:\HTML\viewer\files\check2.zip=[zip", "1]"};
 }
}

输出:

10184010 ----- [C:\HTML\viewer\files\output\files.zip=[zip, 10184010]]
919362 ----- [C:\HTML\viewer\files\output\ViewerJS\commons-io-2.11.0-src (2).zip=[zip, 919362]]
345068 ----- [C:\HTML\viewer\files\jar_files (2).zip=[zip, 345068]]
1 ----- [C:\HTML\viewer\files\check2.zip=[zip, 1]]
1 ----- [ C:\HTML\viewer\files\output\ViewerJS\rar1.rar=[rar, 1]]