如何对含String和Object的Map列表按双日期字段排序
按Date1和Date2字段对Map列表排序的解决方案
没问题!要实现你需要的对List<Map<String, Object>>按Date1升序、Date1相同时按Date2升序的排序需求,我们可以利用Java的比较器机制来定义自定义排序规则,下面分两种方式给你演示,覆盖不同的Java版本:
方式一:Java 8之前的传统Comparator写法
这种写法兼容性好,适合老版本Java项目:
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.text.SimpleDateFormat; public class MapSortDemo { public static void main(String[] args) { // 初始化你的Map列表 List<Map<String, Object>> listOfMap = new ArrayList<>(); Map<String, Object> map1 = new HashMap<>(); map1.put("Number", 2); map1.put("Date1", new Date("12-OCT-2018")); map1.put("Date2", new Date("02-MAY-2017")); Map<String, Object> map2 = new HashMap<>(); map2.put("Number", 5); map2.put("Date1", new Date("12-OCT-2018")); map2.put("Date2", new Date("01-JAN-2017")); Map<String, Object> mapN = new HashMap<>(); mapN.put("Number", 8); mapN.put("Date1", new Date("10-JAN-2016")); mapN.put("Date2", new Date("10-MAY-2016")); listOfMap.add(map1); listOfMap.add(map2); listOfMap.add(mapN); // 定义自定义排序规则 Collections.sort(listOfMap, new Comparator<Map<String, Object>>() { @Override public int compare(Map<String, Object> m1, Map<String, Object> m2) { // 先比较Date1字段 Date date1M1 = (Date) m1.get("Date1"); Date date1M2 = (Date) m2.get("Date1"); int date1Comparison = date1M1.compareTo(date1M2); // 如果Date1相等,再比较Date2字段 if (date1Comparison == 0) { Date date2M1 = (Date) m1.get("Date2"); Date date2M2 = (Date) m2.get("Date2"); return date2M1.compareTo(date2M2); } return date1Comparison; } }); // 按指定格式输出结果 System.out.println("Number Date1 Date2"); SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yy"); for (Map<String, Object> map : listOfMap) { System.out.printf("%d %s %s%n", (Integer) map.get("Number"), dateFormat.format(map.get("Date1")), dateFormat.format(map.get("Date2"))); } } }
方式二:Java 8+的Lambda与Stream API写法
这种写法更简洁,利用Java 8引入的Stream和Comparator的链式调用特性:
import java.util.ArrayList; import java.util.Comparator; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.text.SimpleDateFormat; import java.util.stream.Collectors; public class MapSortStreamDemo { public static void main(String[] args) { // 初始化你的Map列表(和上面一致) List<Map<String, Object>> listOfMap = new ArrayList<>(); Map<String, Object> map1 = new HashMap<>(); map1.put("Number", 2); map1.put("Date1", new Date("12-OCT-2018")); map1.put("Date2", new Date("02-MAY-2017")); Map<String, Object> map2 = new HashMap<>(); map2.put("Number", 5); map2.put("Date1", new Date("12-OCT-2018")); map2.put("Date2", new Date("01-JAN-2017")); Map<String, Object> mapN = new HashMap<>(); mapN.put("Number", 8); mapN.put("Date1", new Date("10-JAN-2016")); mapN.put("Date2", new Date("10-MAY-2016")); listOfMap.add(map1); listOfMap.add(map2); listOfMap.add(mapN); // 链式定义排序规则并生成排序后的列表 List<Map<String, Object>> sortedList = listOfMap.stream() .sorted(Comparator.comparing((Map<String, Object> map) -> (Date) map.get("Date1")) .thenComparing(map -> (Date) map.get("Date2"))) .collect(Collectors.toList()); // Java 16+可简化为.toList() // 输出结果(和上面一致) System.out.println("Number Date1 Date2"); SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yy"); for (Map<String, Object> map : sortedList) { System.out.printf("%d %s %s%n", (Integer) map.get("Number"), dateFormat.format(map.get("Date1")), dateFormat.format(map.get("Date2"))); } } }
额外注意事项
- 类型安全与空值处理:示例中直接强制转换
Object为Date,实际项目中建议添加非空判断(比如if (map.get("Date1") == null)),避免NullPointerException;如果可能,尽量使用泛型来提升类型安全性。 - 过时的Date构造方法:
new Date(String)是Java的过时API,实际开发中推荐使用SimpleDateFormat(或Java 8+的java.timeAPI)来解析日期字符串,示例:SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", java.util.Locale.ENGLISH); Date date = sdf.parse("12-OCT-2018"); - 日期格式化:你可以通过调整
SimpleDateFormat的模式字符串,来匹配你需要的输出格式,比如dd-MMM-yy对应示例中的10-Jan-16格式。
内容的提问来源于stack exchange,提问作者RajeysGS
相关产品推荐
相关产品推荐

