Java map sort by value

I'm looking for a way to sort map < string, integer > By value I found this post, which solved my sorting problem, but not completely According to the post, I wrote the following code:

import java.util.*;

public class Sort {

    static class ValueComparator implements Comparator<String> {

        Map<String,Integer> base;

        ValueComparator(Map<String,Integer> base) {
            this.base = base;
        }

        @Override
        public int compare(String a,String b) {
            if (base.get(a) >= base.get(b)) {
                return 1;
            } else {
                return -1;
            }
        }
    }

    public static void main(String[] args) {
        HashMap<String,Integer> map = new HashMap<String,Integer>();
        ValueComparator vc = new ValueComparator(map);
        TreeMap<String,Integer> sorted = new TreeMap<String,Integer>(vc);
        map.put("A",1);
        map.put("B",2);
        sorted.putAll(map);
        for (String key : sorted.keySet()) {
            System.out.println(key + " : " + sorted.get(key)); // why null values here?
        }
        System.out.println(sorted.values()); // But we do have non-null values here!
    }
}

Output:

A : null
B : null
[1,2]
BUILD SUCCESSFUL (total time: 0 seconds)

As you can see from the output, the get method always returns null The reason is my valuecomparator The compare () method will never return 0. I have made @ L_ 419_ 1@.

It was suggested that in that article, the following null value problems should be solved:

public int compare(String a,String b) {
            if (base.get(a) > base.get(b)) {
                return 1;
            }else if(base.get(a) ==  base.get(b)){
                return 0;
            }
            return -1;  
        }

I have tested this code and introduced a key merge problem In other words, when these values are equal, their corresponding keys are merged

I have also tried the following:

public int compare(String a,String b) {
                if (a.equals(b)) return 0;
                if (base.get(a) >= base.get(b)) {
                    return 1;
                } else return -1;
            }

It doesn't work either Some values are still empty In addition, there may be a logical problem with this solution

Can anyone come up with a comprehensive solution to my problem? I want to sort through the value function and the get method to work at the same time

Solution

In the compare function, when the values are equal, you should compare the keys This ensures that different keys with the same value are not "merged" because it eliminates entries that would otherwise be equal

For example:

@Override
    public int compare(String a,String b) {
        Integer x = base.get(a);
        Integer y = base.get(b);
        if (x.equals(y)) {
            return a.compareTo(b);
        }
        return x.compareTo(y);
    }

(you need to modify the above code to comply with your null Policy)

Please note that your method of ranking values is quite fragile Your sort map will not support adding new entries, which may be confusing

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>