• java根据key或value对Map进行排序


    根据key排序

    java.util包下的TreeMap可以满足此类需求,直接调用TreeMap的构造函数传入我们自己定义的比较器即可

    public static Map sortMapByKey(Map map) {
    	if (map == null || map.isEmpty()) {
        	return null;
        }
    
    	Map sortMap = new TreeMap(
        	new Comparator() {
            	@Override
                public int compare(String o1, String o2) {
                	return o1.compareTo(o2);
                }});
        sortMap.putAll(map);
    	return sortMap;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    根据value排序

    原理:将map中所有的entry元素放在list容器中,调用Collections里的静态方法sort(List list, Comparator< super T> c) 进行排序,重新compare方法定义排序规则。为了保证Map中元素与排序后的List中的元素的顺序一致,使用了LinkedHashMap。

    public static Map sortMapByValue(Map oriMap) {
            if (oriMap == null || oriMap.isEmpty()) {
                return null;
            }
            Map sortedMap = new LinkedHashMap();
            List> entryList = new ArrayList>(oriMap.entrySet());
            Collections.sort(entryList, new Comparator>() {
                @Override
                public int compare(Entry o1, Entry o2) {
    
                    return o1.getValue().getColumnSortNum() - o2.getValue().getColumnSortNum();
                }
            });
    
            Iterator> iter = entryList.iterator();
            Entry tmpEntry = null;
            while (iter.hasNext()) {
                tmpEntry = iter.next();
                sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue());
            }
            return sortedMap;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    ExportExcelColum类

    public class ExportExcelColumn {
    
        private String columnName;
    
        private Integer columnSortNum;
        public ExportExcelColumn() {
    
        }
        public String getColumnName() {
            return columnName;
        }
        public void setColumnName(String columnName) {
            this.columnName = columnName;
        }
        public Integer getColumnSortNum() {
            return columnSortNum;
        }
        public void setColumnSortNum(Integer columnSortNum) {
            this.columnSortNum = columnSortNum;
        }
        public ExportExcelColumn(String columnName, Integer columnSortNum) {
            this.columnName = columnName;
            this.columnSortNum = columnSortNum;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
  • 相关阅读:
    【日积月累】Java开发习惯养成
    7.3EF Core与ASP.NET Core集成
    阿里内部高并发编程高阶笔记终于开源出来了!
    Windows 基于Visual Studio 开发Qt 6 连接MySQL 8
    Android Studio制作简单登录界面
    电脑怎么格式化清除所有数据
    RocketMQ(4.9.4)学习笔记 - 安装部署
    Navisworks二次开发——工具附加模块添加
    详解如何使用LAMP架构搭建论坛
    Java中如何设置注释模板呢?
  • 原文地址:https://blog.csdn.net/m0_54849806/article/details/126595930