zoukankan      html  css  js  c++  java
  • map根据属性排序、取出map前n个

        /**
         * map根据value排序
         * flag = 1 正序
         * flag = 0 倒序
         *
         * @param map
         * @param flag
         * @return
         */
        public static <K, V extends Comparable<? super V>> LinkedHashMap<K, V> sortByValue(Map<K, V> map, int flag) {
            LinkedHashMap<K, V> sortMap = new LinkedHashMap<>();
            if (flag == 1) {
                map.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getValue)).forEach(entry -> sortMap.put(entry.getKey(), entry.getValue()));
            } else {
                map.entrySet().stream().sorted((o1, o2) -> o2.getValue().compareTo(o1.getValue())).forEach(entry -> sortMap.put(entry.getKey(), entry.getValue()));
            }
            return sortMap;
        }
    
        /**
         * 取出map前n个
         *
         * @param map
         * @param length
         * @return
         */
        @Override
        public LinkedHashMap<String, Double> subMap(LinkedHashMap<String, Double> map, int length) {
            List<Map.Entry<String, Double>> lists = new ArrayList<>(map.entrySet());
            LinkedHashMap<String, Double> sortedMap = new LinkedHashMap<>();
            if (lists.size() >= length) {
                for (Map.Entry<String, Double> set : lists.subList(0, length)) {
                    sortedMap.put(set.getKey(), set.getValue());
                }
            } else {
                for (Map.Entry<String, Double> set : lists) {
                    sortedMap.put(set.getKey(), set.getValue());
                }
            }
            return sortedMap;
        }
    

      

  • 相关阅读:
    k8s 1.10 部署springcloud
    TF-IDF关键词提取步骤
    k8s 离线安装
    JWT对SpringCloud进行系统认证和服务鉴权
    centos7 安装 docker-registry
    Docker安装elasticsearch-head监控ES步骤
    tengine 配置应用防火墙
    elasticsearch6.1.3 集成分词器
    centos7 nginx tengine 安装
    elk 搭建
  • 原文地址:https://www.cnblogs.com/sueyyyy/p/11793746.html
Copyright © 2011-2022 走看看