【问题标题】:Sorting numbers String typed in a TreeMap对 TreeMap 中键入的数字字符串进行排序
【发布时间】:2020-05-17 18:11:17
【问题描述】:

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

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

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

此函数将所有数据放入一个列表中,即必须处理的原始数据。 它来自 SQL 查询的“有序 ASC”。

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是重复的次数->频率。

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 dictionary collections treemap


    【解决方案1】:

    Java Map comparingByKey() 按条目的自然顺序对条目进行排序。对于字符串,orderinglexicographic,这意味着字符串是逐字符比较的;如果第一个不匹配的字符在字符串 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));
    

    【讨论】:

    • 我尝试使用自己的比较器,但无法正常工作。我已经测试了您的答案并且也是有效的。谢谢!
    【解决方案2】:

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

    即使您的键中有字符串值而不是整数,下面的代码也能正常工作。 ({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 块。

    【讨论】:

    • 多话!我目前正在学习这些 Java 集合。感谢您的宝贵时间
    • 这种异常的后备行为违反了比较器的传递性要求。最简单的解决方案是不实现无人要求的异常处理。就Comparator.comparingInt(e -&gt; Integer.parseInt(e.getKey()))
    • @Holger True,因此答案中的最后一个注释是删除 catch 块。我不确定 OP 是否意味着 Keys 也可以包含实际的字符串值(the key is a String
    【解决方案3】:

    使用将键转换为整数的比较器按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;
        }
    

    【讨论】:

    • 我在做所有这些工作之前尝试过转换数据,但我测试过它会使其他数据集不适合。然后我去尝试像你这样的东西,但它对我需要的东西来说太复杂了。好主意,我的朋友,虽然我迷失了太多的 lambda 表达式。谢谢你的回答:D
    猜你喜欢
    • 2017-04-10
    • 2016-08-25
    • 2012-11-14
    • 1970-01-01
    • 2020-11-12
    • 2022-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多