TreeMap<String, Integer> object's get method return null value

妖精的绣舞 提交于 2019-12-06 12:23:32

My guess is that your ValueComparator.compare() method never returns 0, indicating equality, causing the Map.get() method to not find matches.

Change your compare to in this way

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

Even with your Comparator which is definitely broken the program will work if you change it as

for (Map.Entry e : sorted.entrySet()) {
    System.out.println(e.getKey() + " : " + e.getValue());
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!