简体   繁体   English

Java TreeMap:无法按长度/值的降序对 TreeMap 键进行排序

[英]Java TreeMap : Unable to sort the TreeMap key in decreasing order of the length/value

I am trying to use the TreeMap to sort my keys, which are being stored in the Map<String,Integer> .我正在尝试使用TreeMap对存储在Map<String,Integer>中的键进行排序。 But for some reason, the keys are not arranged correctly in decreasing order, as intended.但由于某种原因,键没有按预期按降序正确排列。 I would like to know if there is a default way to achieve the intended order of the keys or I need to write some custom method to achieve this?我想知道是否有默认的方法来实现按键的预期顺序,或者我需要编写一些自定义方法来实现这一点?

Following is the sample code I have:以下是我的示例代码:

public class ApplicationMain {
    public static void main(String[] args) {
        final Map<String, Integer> sampleTreeMap = new TreeMap<>();
        sampleTreeMap.put("5903766131", 6);
        sampleTreeMap.put("5903767", 7);
        sampleTreeMap.put("590376614", 5);
        sampleTreeMap.put("5903766170", 9);
        sampleTreeMap.put("59037662", 12);
        sampleTreeMap.put("5903766410", 10);

        sampleTreeMap.entrySet().stream().forEach(entry ->{
            System.out.println("Key : " + entry.getKey() + " -- " + entry.getValue());
        });
    }
}

The following is the output I am getting:以下是我收到的 output:

Key : 5903766131 -- 6
Key : 590376614 -- 5
Key : 5903766170 -- 9
Key : 59037662 -- 12
Key : 5903766410 -- 10
Key : 5903767 -- 7

I would like the output to be in descending order of the keys, so a larger number with a higher number of digits or characters would appear at the top, then a lower number with fewer digits.我希望 output 按键的降序排列,因此数字或字符数较多的较大数字会出现在顶部,然后是数字较少的较小数字。 Something like this:是这样的:

Key : 5903766410 -- 10
Key : 5903766170 -- 9
Key : 5903766131 -- 6
Key : 590376614 -- 5
Key : 59037662 -- 12
Key : 5903767 -- 7

Note:笔记:

  1. I cannot change my data type to Integer or Long as this is coming from another application, and I would like to use it as a string in further processing, so I would like to find a way in which I can sort them properly.我无法将我的数据类型更改为 Integer 或 Long,因为这是来自另一个应用程序,我想在进一步处理中将其用作字符串,因此我想找到一种可以正确排序它们的方法。

  2. I was previously using HashMap, but after discovering that it does not support ordering, I switched to TreeMap.之前用的是HashMap,后来发现不支持排序,转用TreeMap。

Please provide some suggestions on how to fix the issue.请提供一些有关如何解决该问题的建议。

It looks like you want to sort the keys as numbers instead of strings.看起来您想将键排序为数字而不是字符串。 One way to do this is to provide a custom Сomparator when creating the map.一种方法是在创建Сomparator时提供自定义比较器。

final Map<String, Integer> sampleTreeMap =
                new TreeMap<>((Comparator.comparingLong((String s) -> Long.parseLong(s))).reversed());

PS You might find it helpful to read this question Comparator.comparing().reversed() strange behaviour / not working as expected PS 你可能会发现阅读这个问题Comparator.comparing().reversed() strange behavior / not working as expected很有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM