加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 在根据值对地图进行排序时,缺少某些值.是什么导致这种奇

发布时间:2020-12-14 05:46:35 所属栏目:Java 来源:网络整理
导读:我试图根据词频(即基于值)对地图进行排序.为此,我已经覆盖了比较器并传递给TreeMap,但我得到了这个奇怪的输出. public class WordFrequency { public static String sentence = "one three two two three three four four four"; public static MapString,In
我试图根据词频(即基于值)对地图进行排序.为此,我已经覆盖了比较器并传递给TreeMap,但我得到了这个奇怪的输出.
public class WordFrequency {
    public static String sentence = "one three two two three three four four four";
    public static Map<String,Integer> map;

    public static void main(String[] args) {
        map = new HashMap<>();
        String[] words = sentence.split("s");

        for (String word : words) {
            Integer count = map.get(word);
            if (count == null) {
                count = 1;
            } else {
                ++count;
            }
            map.put(word,count);
        }

        Comparator<String> myComparator = new Comparator<String>() {

            @Override
            public int compare(String s1,String s2) {
                if (map.get(s1) < map.get(s2)) {
                    return -1;
                } else if (map.get(s1) > map.get(s2)) {
                    return 1;
                } else {
                    return 0;
                }
            }

        };
        SortedMap<String,Integer> sortedMap = new TreeMap<String,Integer>(myComparator);
        System.out.println("Before sorting: " + map);
        sortedMap.putAll(map);
        System.out.println("After Sorting based on value:" + sortedMap);

    }
}

输出:

Before sorting: {two=2,one=1,three=3,four=3}
After sorting based on value:{one=1,two=2,three=3}

预期产出:

{one=1,four=3,three=3}

解决方法

您的比较方法无法遵守Map接口的约定,因为它比较值而不是键.您的实现会导致具有相同值的两个键被视为相同的键.因此,您的sortedMap不包含“四”键,其值与“三”键相同.

Note that the ordering maintained by a tree map,like any sorted map,and whether or not an explicit comparator is provided,must be consistent with equals if this sorted map is to correctly implement the Map interface. (See Comparable or Comparator for a precise definition of consistent with equals.) This is so because the Map interface is defined in terms of the equals operation,but a sorted map performs all key comparisons using its compareTo (or compare) method,so two keys that are deemed equal by this method are,from the standpoint of the sorted map,equal. The behavior of a sorted map is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Map interface.

TreeMap reference

您可以通过在值相等时比较键来解决此问题:

Comparator<String> myComparator = new Comparator<String>() {

        @Override
        public int compare(String s1,String s2) {
            if (map.get(s1) < map.get(s2)) {
                return -1;
            } else if (map.get(s1) > map.get(s2)) {
                return 1;
            } else {
                return s1.compareTo(s2);
            }
        }

    };

这应该给你一个输出:

After sorting based on value:{one=1,three=3}

由于四个<三个基于字符串的自然顺序.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读