zoukankan      html  css  js  c++  java
  • Map排序工具类

    package com.csf.sam.util;
    
    import java.util.*;
    
    /**
     * Created by fenglei.ma on 2018/4/23. 23:03
     */
    public class CollectionTools {
    
        /**
         * 将map按照value值排序
         *
         * @param map
         * @param reverse true:倒序。false:正序。
         * @return
         */
        @SuppressWarnings({ "rawtypes", "unchecked" })
        public static Map sortByValue(Map map, final boolean reverse) {
            List list = new LinkedList(map.entrySet());
            Collections.sort(list, new Comparator() {
                public int compare(Object o1, Object o2) {
                    if (reverse) {
                        return -((Comparable) ((Map.Entry) o1).getValue())
                                .compareTo(((Map.Entry) o2).getValue());
                    }
                    return ((Comparable) ((Map.Entry) o1).getValue())
                            .compareTo(((Map.Entry) o2).getValue());
                }
            });
    
            Map result = new LinkedHashMap();
            for (Iterator it = list.iterator(); it.hasNext(); ) {
                Map.Entry entry = (Map.Entry) it.next();
                result.put(entry.getKey(), entry.getValue());
            }
            return result;
        }
    
        /**
         * 将map按照key值排序
         *
         * @param map
         * @param reverse true:倒序。false:正序。
         * @return
         */
        @SuppressWarnings({ "unchecked", "rawtypes" })
        public static Map sortByKey(Map map, final boolean reverse) {
            List list = new LinkedList(map.entrySet());
            Collections.sort(list, new Comparator() {
                public int compare(Object o1, Object o2) {
                    if (reverse) {
                        return -((Comparable) ((Map.Entry) o1).getKey())
                                .compareTo(((Map.Entry) o2).getKey());
                    }
                    return ((Comparable) ((Map.Entry) o1).getKey())
                            .compareTo(((Map.Entry) o2).getKey());
                }
            });
    
            Map result = new LinkedHashMap();
            for (Iterator it = list.iterator(); it.hasNext(); ) {
                Map.Entry entry = (Map.Entry) it.next();
                result.put(entry.getKey(), entry.getValue());
            }
            return result;
        }
    
    
        public static void main(String[] args) {
            Map<Integer, Integer> map = new HashMap<Integer, Integer>();
            map.put(1, 1);
            map.put(3, 3);
            map.put(2, 2);
    
            Map result = sortByKey(map, true);
            System.out.println(result);
            System.out.println(result.keySet());
    
            List<Integer> list = new ArrayList<Integer>();
            list.addAll(result.keySet());
            System.out.println(list);
        }
    }
  • 相关阅读:
    VMware Workstation 卸载时卡在“正在卸载网络驱动程序(Virtual Network Editor夯死)”
    Windows 开启 winrm
    【Git】error: RPC failed; HTTP 413 curl 22 The requested URL returned error:413 Request Entity Too Large
    tricks
    MySQL 5.7原生通用二进制格式安装包安装过程
    WebSnapshotsHelper(HTML转换为图片)
    使用两个zTree,调用一个onCheck函数,分别展开不同的节点
    修改Sql Server字段类型
    添加一条公告并标记为已读
    优化部门人员加载缓慢
  • 原文地址:https://www.cnblogs.com/xiaolei2017/p/8929951.html
Copyright © 2011-2022 走看看