Java中 List 集合,通过 Stream 流进行排序总结
创始人
2024-12-03 01:02:39
0

一、数据准备

public class OrderTest {     private String channelCode;     private BigDecimal rate;     // 省略 getter、setter、toString()、constructor } 
		List orderTestList = new ArrayList<>();         OrderTest z09 = new OrderTest("Z09", new BigDecimal("7.6677"));         OrderTest B2C = new OrderTest("B2C", new BigDecimal("5.6666"));         OrderTest Z04 = new OrderTest("Z04", new BigDecimal("4.3137"));         OrderTest ALL = new OrderTest("ALL", new BigDecimal("4.3137"));         OrderTest ALL1 = new OrderTest("ALL", new BigDecimal("4.5137"));         // 演示多字段排序使用         OrderTest z091 = new OrderTest("Z09", new BigDecimal("7.6671"));         // 演示给 null 值排序用         OrderTest z092 = new OrderTest("Z09", null);         OrderTest B2C1 = new OrderTest("B2C", new BigDecimal("5.6666"));         OrderTest Z041 = new OrderTest("Z04", null);         orderTestList.add(z09);         orderTestList.add(z091);         orderTestList.add(B2C);         orderTestList.add(Z04);         orderTestList.add(ALL);         orderTestList.add(ALL1); 

二、单字段排序

2.1、升序

 list.stram().sorted(Comparator.Comparing(YourClass::Class's Field) 
System.out.println("----------------------------------------------");         System.out.println("只按照 channelCode 升序排序:");         List channelAsc =                 orderTestList.stream()                         .sorted(Comparator.comparing(OrderTest::getChannelCode))                         .collect(Collectors.toList());         channelAsc.forEach(System.out::println); 

结果:
在这里插入图片描述

2.2、降序

 list.stram().sorted(Comparator.Comparing(YourClass::Class's Field, Comparator.reverseOrder()) 
System.out.println("----------------------------------------------");         System.out.println("只按照 channelCode 降序排序:");         List channelDesc =                 orderTestList.stream()                         .sorted(Comparator.comparing(OrderTest::getChannelCode, Comparator.reverseOrder()))                         .collect(Collectors.toList());         channelDesc.forEach(System.out::println); 

结果:
在这里插入图片描述

二、多字段排序

利用的是 thenComparing(): 升序 thenComparing(YourClass::Class's Field), 降序 thenComparing(YourClass::Class's Field, Comparator.reverseOrder())。  注意: 使用 thenComparing(YourClass::Class's Field).reversed() 的时候要注意排序要求, 如果先按照 A 字段升序 B 字段升序的话,使用 reversed() 之后的结果是对 A 降序 B 降序。 
System.out.println("----------------------------------------------");         System.out.println("先按照 channelCode 升序,再按照 rate 升序排序:");         List channelCodeAscRateAscList =                 orderTestList.stream()                         .sorted(Comparator.comparing(OrderTest::getChannelCode)                                 .thenComparing(OrderTest::getRate))                         .collect(Collectors.toList());         channelCodeAscRateAscList.forEach(System.out::println); 

结果
在这里插入图片描述
先按照 channelCode 将序,再按照 rate 升序将序,使用 reversed():

System.out.println("----------------------------------------------");         System.out.println("先按照 channelCode 将序,再按照 rate 将序排序,使用 reversed():");         List channelCodeAscRateAscWithReversedList =                 orderTestList.stream()                         .sorted(Comparator.comparing(OrderTest::getChannelCode)                                 .thenComparing(OrderTest::getRate).reversed())                         .collect(Collectors.toList());         channelCodeAscRateAscWithReversedList.forEach(System.out::println); 

结果
在这里插入图片描述

三、对 null 值处理

 Comparator.nullsFirst(Comparator.reverseOrder())  -- null排在前面,reverseOrder是倒序,升序用naturalOrder  Comparator.nullsLast(Comparator.reverseOrder())   -- null排在后面,reverseOrder是倒序,升序用naturalOrder 
orderTestList.add(new OrderTest(("Z09")));         orderTestList.add(new OrderTest(("B2C")));         orderTestList.add(new OrderTest(("Z04")));         System.out.println("----------------------------------------------");         System.out.println("先按照 channelCode 升序,再按照 rate 降序并且 null 值放前面排序:");         List channelCodeAscRateDescNullFirstList = orderTestList.stream()                 .sorted(Comparator.comparing(OrderTest::getChannelCode)                         .thenComparing(OrderTest::getRate, Comparator.nullsFirst(Comparator.reverseOrder())))                 .collect(Collectors.toList());         channelCodeAscRateDescNullFirstList.forEach(System.out::println); 

结果
在这里插入图片描述

四、对排序字段个数不固定的情况,如何排序

需求:排序字段个数不确定,如何实现动态排序?

伪代码示例:

		Comparator comparing = null;         for (int i = 0; i < dictItemList.size(); i++) {             String cd = dictItemList.get(i).getCd();             if (i == 0) {                 // 首次排序                 comparing = getComparingByDictCd(cd, null);             } else {             	// 第二次排序及以上                 comparing = getComparingByDictCd(cd, comparing);             }         }         // comparing 比较器,保存了排序的规则,responseList 是被排序的集合 		responseList = responseList.stream().sorted(comparing) 						.collect(Collectors.toList()); 		 		// getComparingByDictCd 方法 		private Comparator getComparingByDictCd(String dictCd,  Comparator result = null;         switch (dictCd) {             case "age":                 result = comparing == null ? Comparator.comparing(SurgUnArrangeResponse::getAge, Comparator.nullsLast(Comparator.reverseOrder())) :                         comparing.thenComparing(SurgUnArrangeResponse::getAge, Comparator.nullsLast(Comparator.reverseOrder()));                 break;                 ....         	}         	return result; 		} 

相关内容

热门资讯

目前来看!aapoker破解侠... 目前来看!aapoker破解侠是真的吗,aapoker发牌逻辑(透视)指南教程(果然有挂)-哔哩哔哩...
透视项目!wepoker破解工... 透视项目!wepoker破解工具,wepoker的辅助器(脚本)了解教程(总是真的是有挂)-哔哩哔哩...
透视解密!wpk俱乐部是真的吗... 透视解密!wpk俱乐部是真的吗(透视)wpk透视是真的吗,教程秘籍(有挂技巧)-哔哩哔哩进入游戏-大...
近期!aapoker怎么设置提... 近期!aapoker怎么设置提高好牌几率,aapoker破解侠是真的吗(透视)法门教程(原来真的是有...
透视模板!wejoker黑侠辅... 透视模板!wejoker黑侠辅助器,wepoker辅助器如何使用(脚本)了解教程(其实真的有挂)-哔...
透视了解!如何判断wpk辅助软... 透视了解!如何判断wpk辅助软件的真假(透视)wpk德州局怎么透视,教程手段(有挂透视)-哔哩哔哩;...
透视手筋!wepoker到底有... 透视手筋!wepoker到底有没有透视,wepoker免费透视(脚本)科普教程(都是真的是有挂)-哔...
有消息称!aapoker辅助软... 有消息称!aapoker辅助软件合法吗,aapoker怎么设置提高好牌几率(透视)机巧教程(本来是真...
透视普及!wpk辅助(透视)w... 透视普及!wpk辅助(透视)wpk透视辅助下载,教程模板(有挂方式)-哔哩哔哩1、起透看视 wpk透...
透视要领!wepoker私人局... 透视要领!wepoker私人局有透视吗,wepoker透视有用吗(脚本)解谜教程(其实是有挂)-哔哩...