对map排序,用TreeSet(Comparator)排序,比用Collections.sort() || list.sort排序速度更快

做一个题,统计字符串中字母出现次数并排序。

使用sort()排序


    // 统计字符串有多少重复值
    public static void countMap() {

        long start = System.currentTimeMillis();

        // 定义字符串
        String str = "asldkqiycaxhckqowiudockashdiqwdyaiyrdcea";

        // 定义map
        Map<Character, Integer> map = new LinkedHashMap<>();

        // 遍历字符串,获取值并存入map
        for (int i = 0; i < str.length(); i++) {
            char s = str.charAt(i);
            if (map.get(str.charAt(i)) != null) {
                // map中有值
                map.put(s, map.get(s) + 1);
            }else {
                // map中无值
                map.put(s, 1);
            }
        }

        // 对map进行排序
        // 将map转换成list
        List<Map.Entry<Character, Integer>> list = new ArrayList<>(map.entrySet());

        // 对list进行降序排序,如果值相同,对键进行升序排序
        list.sort((o1, o2) -> {
            int result = o2.getValue().compareTo(o1.getValue());

            return result == 0 ? o1.getKey().compareTo(o2.getKey()) : result;
        });

        // 清空原有map信息
        map.clear();

        for (Map.Entry<Character, Integer> m : list) {
            map.put(m.getKey(), m.getValue());
        }

        // 输出新map
        System.out.println(map);

        long end = System.currentTimeMillis();

        System.out.println("List总耗时:" + (end - start));
    }

创建TreeSet(Comparator)进行排序


    // set统计字符串并排序
    public static void countSet() {

        long start = System.currentTimeMillis();

        // 定义字符串
        String str = "asldkqiycaxhckqowiudockashdiqwdyaiyrdcea";

        // 定义map
        Map<Character, Integer> map = new LinkedHashMap<>();

        // 遍历字符串,获取值并存入map
        for (int i = 0; i < str.length(); i++) {
            char s = str.charAt(i);
            if (map.get(str.charAt(i)) != null) {
                // map中有值
                map.put(s, map.get(s) + 1);
            } else {
                // map中无值
                map.put(s, 1);
            }
        }

        // 统计重复元素
        TreeSet<Map.Entry<Character, Integer>> treeSet = new TreeSet<>(new Comparator<Map.Entry<Character, Integer>>() {
            @Override
            public int compare(Map.Entry<Character, Integer> o1, Map.Entry<Character, Integer> o2) {

                int result = o2.getValue() - o1.getValue();

                return result == 0 ? o1.getKey() - o2.getKey() : result;
            }
        });

        treeSet.addAll(map.entrySet());

        // 清空map原有信息
        map.clear();

        for (Map.Entry<Character, Integer> m : treeSet) {
            map.put(m.getKey(), m.getValue());
        }

        System.out.println(map);

        long end = System.currentTimeMillis();

        System.out.println("Set总耗时:" + (end - start));
    }
}

在main中运行


    public static void main(String[] args) {
        System.out.println("统计排序:\n");
        countMap();
        System.out.println();
        countSet();
    }

结果:

可以看出,对map集合进行排序时,通过TreeSet方式排序是比较快的。

猜想:

1、一个是所有值插入后,遍历list进行排序;一个是在插值时进行排序,所以TreeSet方式更快。

2、Set集合与Map集合键的特性一致,都是不允许重复值存在,所以Set集合排序更快?

具体原因只能等以后在研究了,先将就着用哈哈哈哈。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值