对 TreeMap 中键入的数字字符串进行排序

Sorting numbers String typed in a TreeMap

我有一组数据,在绘图之前我想重新组织这些数据。

数据由一堆重复的数字和您计算频率的数字组成。然后,在另一个函数中,我尝试按升序数字顺序重新组织转换后的数据(重新组织频率)。

Java 有很多工具,但我一直没有成功,因为密钥是一个被视为字符串的数字(我知道这很奇怪,但我无法更改它...)

该函数将所有数据获取到一个列表中,必须处理的原始数据。 它来自 "ordered ASC" 来自 SQL 查询。

public Map<String, Integer> getDurations(){

        EntityManager em = getEntityManager();

        try {
            System.out.println("Building query");
            //Query query = em.createNativeQuery("Data.prices", String.class);

            Query spm = em.createQuery("SELECT d.length FROM Data d ORDER BY d.length ASC"); 

            List<String> durationList = new ArrayList<>();

            //Load output to a list
            for (Object item : spm.getResultList()){
                durationList.add(item+"");
            }
            //Find the frecuency for later dynamically express the data into the chart
            System.out.println(durationList.toString());
            return findFrecuency(durationList);
        } finally {
            em.close();
        }        
    }

但是一旦处理... key是List中前面重复的次数,value是重复次数-> frecuency

private Map<String, Integer> findFrecuency(List<String> data){

        Map<String,Integer> frecuencyTable = new HashMap<>();

        for(String d : data){

            if(!frecuencyTable.containsKey(d)){
                frecuencyTable.put(d,1);
            }else{
                frecuencyTable.put(d, frecuencyTable.get(d)+1);
            }
        }        
        //Reorganize the data before export
        frecuencyTable = frecuencyTable
                .entrySet()
                .stream()
                .sorted(comparingByKey()) 
                .collect(toMap(e -> e.getKey(), e -> e.getValue(), (e1, e2) -> e2, LinkedHashMap::new));

        System.out.println("Resulting frecuency is: " + frecuencyTable);



        return frecuencyTable;
    }

我已经尝试过这个很酷的功能

frecuencyTable = frecuencyTable
                .entrySet()
                .stream()
                .sorted(comparingByKey()) 
                .collect(toMap(e -> e.getKey(), e -> e.getValue(), (e1, e2) -> e2, LinkedHashMap::new));

但是最后的数据是这样的...

Resulting frecuency is: {100=12, 101=7, 102=11, 103=9, 104=6, 105=6, 106=6, 107=10, 108=5, 109=7, 110=9, 111=7, 112=13, 113=8, 114=10, 115=7, 116=4, 117=5, 118=8, 119=6, 120=9, 121=8, 122=11, 123=7, 124=4, 125=7, 126=9, 127=5, 128=7, 129=7, 130=6, 131=5, 132=8, 133=5, 134=5, 135=10, 136=9, 137=9, 138=6, 139=11, 140=5, 141=7, 142=7, 143=7, 144=8, 145=7, 146=5, 147=9, 148=7, 149=6, 150=8, 151=7, 152=9, 153=9, 154=7, 155=6, 156=4, 157=6, 158=6, 159=6, 160=6, 161=9, 162=5, 163=7, 164=4, 165=5, 166=5, 167=7, 168=4, 169=6, 170=4, 171=8, 172=8, 173=7, 174=6, 175=6, 176=10, 177=6, 178=10, 179=13, 180=7, 181=10, 182=6, 183=5, 184=8, 185=10, 46=5, 47=7, 48=11, 49=5, 50=9, 51=7, 52=7, 53=9, 54=6, 55=2, 56=5, 57=7, 58=7, 59=9, 60=8, 61=10, 62=6, 63=9, 64=9, 65=7, 66=2, 67=8, 68=5, 69=6, 70=7, 71=7, 72=4, 73=12, 74=12, 75=10, 76=7, 77=6, 78=6, 79=6, 80=10, 81=3, 82=7, 83=6, 84=13, 85=17, 86=5, 87=6, 88=4, 89=6, 90=5, 91=6, 92=11, 93=8, 94=4, 95=2, 96=2, 97=4, 98=8, 99=8}

来自原始数据

[46, 46, 46, 46, 46, 47, 47, 47, 47, 47, 47, 47, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 49, 49, 49, 49, 49, 50, 50, 50, 50, 50, 50, 50, 50, 50, 51, 51, 51, 51, 51, 51, 51, 52, 52, 52, 52, 52, 52, 52, 53, 53, 53, 53, 53, 53, 53, 53, 53, 54, 54, 54, 54, 54, 54, 55, 55, 56, 56, 56, 56, 56, 57, 57, 57, 57, 57, 57, 57, 58, 58, 58, 58, 58, 58, 58, 59, 59, 59, 59, 59, 59, 59, 59, 59, 60, 60, 60, 60, 60, 60, 60, 60, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 62, 62, 62, 62, 62, 62, 63, 63, 63, 63, 63, 63, 63, 63, 63, 64, 64, 64, 64, 64, 64, 64, 64, 64, 65, 65, 65, 65, 65, 65, 65, 66, 66, 67, 67, 67, 67, 67, 67, 67, 67, 68, 68, 68, 68, 68, 69, 69, 69, 69, 69, 69, 70, 70, 70, 70, 70, 70, 70, 71, 71, 71, 71, 71, 71, 71, 72, 72, 72, 72, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 76, 76, 76, 76, 76, 76, 76, 77, 77, 77, 77, 77, 77, 78, 78, 78, 78, 78, 78, 79, 79, 79, 79, 79, 79, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 81, 81, 81, 82, 82, 82, 82, 82, 82, 82, 83, 83, 83, 83, 83, 83, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 86, 86, 86, 86, 86, 87, 87, 87, 87, 87, 87, 88, 88, 88, 88, 89, 89, 89, 89, 89, 89, 90, 90, 90, 90, 90, 91, 91, 91, 91, 91, 91, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 93, 93, 93, 93, 93, 93, 93, 93, 94, 94, 94, 94, 95, 95, 96, 96, 97, 97, 97, 97, 98, 98, 98, 98, 98, 98, 98, 98, 99, 99, 99, 99, 99, 99, 99, 99, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 101, 101, 101, 101, 101, 101, 101, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 103, 103, 103, 103, 103, 103, 103, 103, 103, 104, 104, 104, 104, 104, 104, 105, 105, 105, 105, 105, 105, 106, 106, 106, 106, 106, 106, 107, 107, 107, 107, 107, 107, 107, 107, 107, 107, 108, 108, 108, 108, 108, 109, 109, 109, 109, 109, 109, 109, 110, 110, 110, 110, 110, 110, 110, 110, 110, 111, 111, 111, 111, 111, 111, 111, 112, 112, 112, 112, 112, 112, 112, 112, 112, 112, 112, 112, 112, 113, 113, 113, 113, 113, 113, 113, 113, 114, 114, 114, 114, 114, 114, 114, 114, 114, 114, 115, 115, 115, 115, 115, 115, 115, 116, 116, 116, 116, 117, 117, 117, 117, 117, 118, 118, 118, 118, 118, 118, 118, 118, 119, 119, 119, 119, 119, 119, 120, 120, 120, 120, 120, 120, 120, 120, 120, 121, 121, 121, 121, 121, 121, 121, 121, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 123, 123, 123, 123, 123, 123, 123, 124, 124, 124, 124, 125, 125, 125, 125, 125, 125, 125, 126, 126, 126, 126, 126, 126, 126, 126, 126, 127, 127, 127, 127, 127, 128, 128, 128, 128, 128, 128, 128, 129, 129, 129, 129, 129, 129, 129, 130, 130, 130, 130, 130, 130, 131, 131, 131, 131, 131, 132, 132, 132, 132, 132, 132, 132, 132, 133, 133, 133, 133, 133, 134, 134, 134, 134, 134, 135, 135, 135, 135, 135, 135, 135, 135, 135, 135, 136, 136, 136, 136, 136, 136, 136, 136, 136, 137, 137, 137, 137, 137, 137, 137, 137, 137, 138, 138, 138, 138, 138, 138, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 140, 140, 140, 140, 140, 141, 141, 141, 141, 141, 141, 141, 142, 142, 142, 142, 142, 142, 142, 143, 143, 143, 143, 143, 143, 143, 144, 144, 144, 144, 144, 144, 144, 144, 145, 145, 145, 145, 145, 145, 145, 146, 146, 146, 146, 146, 147, 147, 147, 147, 147, 147, 147, 147, 147, 148, 148, 148, 148, 148, 148, 148, 149, 149, 149, 149, 149, 149, 150, 150, 150, 150, 150, 150, 150, 150, 151, 151, 151, 151, 151, 151, 151, 152, 152, 152, 152, 152, 152, 152, 152, 152, 153, 153, 153, 153, 153, 153, 153, 153, 153, 154, 154, 154, 154, 154, 154, 154, 155, 155, 155, 155, 155, 155, 156, 156, 156, 156, 157, 157, 157, 157, 157, 157, 158, 158, 158, 158, 158, 158, 159, 159, 159, 159, 159, 159, 160, 160, 160, 160, 160, 160, 161, 161, 161, 161, 161, 161, 161, 161, 161, 162, 162, 162, 162, 162, 163, 163, 163, 163, 163, 163, 163, 164, 164, 164, 164, 165, 165, 165, 165, 165, 166, 166, 166, 166, 166, 167, 167, 167, 167, 167, 167, 167, 168, 168, 168, 168, 169, 169, 169, 169, 169, 169, 170, 170, 170, 170, 171, 171, 171, 171, 171, 171, 171, 171, 172, 172, 172, 172, 172, 172, 172, 172, 173, 173, 173, 173, 173, 173, 173, 174, 174, 174, 174, 174, 174, 175, 175, 175, 175, 175, 175, 176, 176, 176, 176, 176, 176, 176, 176, 176, 176, 177, 177, 177, 177, 177, 177, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 179, 179, 179, 179, 179, 179, 179, 179, 179, 179, 179, 179, 179, 180, 180, 180, 180, 180, 180, 180, 181, 181, 181, 181, 181, 181, 181, 181, 181, 181, 182, 182, 182, 182, 182, 182, 183, 183, 183, 183, 183, 184, 184, 184, 184, 184, 184, 184, 184, 185, 185, 185, 185, 185, 185, 185, 185, 185, 185]

键 185 之后转到键 46,我不知道如何解决这个问题。

我的目标是有一个上升的键顺序,但键是一个字符串。

Java 地图 comparingByKey() 按条目的自然顺序对条目进行排序。对于字符串,ordering is lexicographic 表示字符串逐个字符进行比较;如果第一个不匹配的字符出现在字符串 B 中位置相似的字符之前,则字符串 A 出现在字符串 B 之前,或者,如果所有字符都匹配,并且 A 比 B 短。

尽管 46100 之前,但 "46""100" 之后,因为 '4''1' 之后。

这里有两个选择:

  1. 您可以在所有两位数号码前加上 "0"。我建议不要这样做,因为它需要额外的逻辑,如果您最终需要 4 位数的密钥,并且您显示的输出将与保存在数据库中的实际值不匹配,这可能会导致错误(或一般混淆)。
  2. 写一个自定义 Comparator 来解释不同长度的字符串 ("shortlex order")。
static final Comparator<String> SHORTLEX_ORDER = 
    new Comparator<String>() {
        public int compare(String a, String b) {
            if (a.length() != b.length()) {
                return a.length() < b.length() ? -1 : 1;
            }

            return a.compareTo(b);
        }
    };

然后像这样使用它

frecuencyTable = frecuencyTable
                .entrySet()
                .stream()
                .sorted(comparingByKey(SHORTLEX_ORDER)) 
                .collect(toMap(e -> e.getKey(), e -> e.getValue(), (e1, e2) -> e2, LinkedHashMap::new));

您可以编写一个自定义比较器来检查该值是字符串还是整数并进行相应比较。

即使您的键中有字符串值而不是整数,以下代码也能正常工作。 ({1=3, 12=1, a=1, kishore=1, 46=2, 48=1})

    frecuencyTable = frecuencyTable
            .entrySet()
            .stream()
            .sorted((e1, e2) -> {
        try {
            return Integer.compare(Integer.parseInt(e1.getKey()), Integer.parseInt(e2.getKey()));
        } catch (NumberFormatException e) {
            return e1.getValue().compareTo(e2.getValue());
        }
    }).collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue(), (e1, e2) -> e2, LinkedHashMap::new));

如果您确定您的密钥始终是存储为字符串的整数,您可以删除 Catch 块。

使用将键转换为整数的比较器按 key

排序
Comparator<Entry<String, Integer>> cmp = (e1, e2) -> Integer.valueOf(e1.getKey())
                .compareTo(Integer.valueOf(e2.getKey()));

,如果要按frequency then by key

排序
Comparator<Entry<String, Integer>> cmp = Entry.<String, Integer>comparingByValue()
                .thenComparing(k -> Integer.valueOf(k.getKey()));

,按键排序示例:

    static Map<String, Integer> findFrecuency(List<String> data) {

        Map<String, Integer> frecuencyTable = new HashMap<>();

        for (String d : data) 
            frecuencyTable.put(d, frecuencyTable.getOrDefault(d, 0) + 1);

        Comparator<Entry<String, Integer>> cmp = (e1, e2) -> Integer.valueOf(e1.getKey())
                .compareTo(Integer.valueOf(e2.getKey()));

        frecuencyTable = frecuencyTable.entrySet().stream().sorted(cmp)
                .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue(), (e1, e2) -> e2, LinkedHashMap::new));

        return frecuencyTable;
    }