Search code examples
javadictionarycollectionstreemap

Sorting numbers String typed in a TreeMap


I have a set of data that before plotting I wanted to reorganize.

The data consists in a bunch of repeated numbers and from those you calculate the frecuency. Then, in another function, I try to reorganize the transformed data (reoganize the frecuencies) in ascendal numeral order.

Java has many tools for that but I've been unsuccesful due to that the key is a number treated as a String (I know it's weird, but I can't change it...)

This function gets all the data into a list, raw data that must be processed. It comes "ordered ASC" from the SQL query.

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();
        }        
    }

But once it is processed... The key is the previous number repeated in the List and the value is the number of repetitions -> 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;
    }

I have tried this cool function which works

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

but the final data looks like this...

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}

from raw data

[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]

After key 185 it goes to key 46 and I am I do not know how to work this around.

My goal is to have an ascendant key order, but the key is a String.


Solution

  • You can write a custom comparator to check if the value is string or int and compare accordingly.

    The below code works even if you've String Values instead of integers inside your Key. ({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));
    

    If you're sure your keys are always Integers stored as string, you can remove the Catch Block.