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);
        }
    }
  • 相关阅读:
    winform,WPF 释放内存垃圾,减少资源占用方法
    Winform中使用WPF控件并动态读取Xaml
    Winform程序双向绑定
    STM32L15XXX 入门笔记
    STM32固件库下载地址
    C#实现虚拟控件列表显示100w个控件方法
    DotNetBar滚动条的疑似BUG
    VS Sln图标空白修复办法
    Swift下使用Xib设计界面
    关于Mac OS虚拟机下共享文件夹的方法
  • 原文地址:https://www.cnblogs.com/xiaolei2017/p/8929951.html
Copyright © 2011-2022 走看看