返回顶部
首页 > 资讯 > 后端开发 > JAVA >Java 字段 Stream 排序
  • 667
分享到

Java 字段 Stream 排序

javastream排序 2023-09-26 05:09:38 667人浏览 安东尼
摘要

这篇博客简单记录下 Java 字段 Stream 排序常用方式,欢迎阅读斧正。 用到的类如下: public class OrderTest { private String channelCo

这篇博客简单记录下 Java 字段 Stream 排序常用方式,欢迎阅读斧正。

用到的类如下:

public class OrderTest {    private String channelCode;    private BigDecimal rate;    // 省略 getter、setter、toString()、constructor}

准备数据:

        List<OrderTest> 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);

单字段

升序使用 .sorted(Comparator.Comparing(YourClass::Class's Field) 就行,降序使用 .sorted(Comparator.Comparing(YourClass::Class's Field, Comparator.reverseOrder())

        System.out.println("----------------------------------------------");        System.out.println("只按照 channelCode 升序排序:");        List<OrderTest> channelAsc =                orderTestList.stream()                        .sorted(Comparator.comparing(OrderTest::getChannelCode))                        .collect(Collectors.toList());        channelAsc.forEach(System.out::println);        System.out.println("----------------------------------------------");        System.out.println("只按照 channelCode 降序排序:");        List<OrderTest> channelDesc =                orderTestList.stream()                        .sorted(Comparator.comparing(OrderTest::getChannelCode, Comparator.reverseOrder()))                        .collect(Collectors.toList());        channelDesc.forEach(System.out::println);

输出:

----------------------------------------------只按照 channelCode 升序排序:OrderTest{channelCode='ALL', rate=4.3137}OrderTest{channelCode='ALL', rate=4.5137}OrderTest{channelCode='B2C', rate=5.6666}OrderTest{channelCode='Z04', rate=4.3137}OrderTest{channelCode='Z09', rate=7.6677}OrderTest{channelCode='Z09', rate=7.6671}----------------------------------------------只按照 channelCode 降序排序:OrderTest{channelCode='Z09', rate=7.6677}OrderTest{channelCode='Z09', rate=7.6671}OrderTest{channelCode='Z04', rate=4.3137}OrderTest{channelCode='B2C', rate=5.6666}OrderTest{channelCode='ALL', rate=4.3137}OrderTest{channelCode='ALL', rate=4.5137}

多字段

利用的是 thenComparing():升序 thenComparing(YourClass::Class's Field),降序 thenComparing(YourClass::Class's Field, Comparator.reverseOrder())

注意: 使用 thenComparing(YourClass::Class's Field).reversed() 的时候要注意排序要求,如果先按照 A 字段升序 B 字段升序的话,使用 reversed() 之后的结果是对 A 降序 B 降序。

不对 null 值处理

先按照 channelCode 升序,再按照 rate 升序排序:

        System.out.println("----------------------------------------------");        System.out.println("先按照 channelCode 升序,再按照 rate 升序排序:");        List<OrderTest> channelCodeAscRateAscList =                orderTestList.stream()                        .sorted(Comparator.comparing(OrderTest::getChannelCode)    .thenComparing(OrderTest::getRate))                        .collect(Collectors.toList());        channelCodeAscRateAscList.forEach(System.out::println);

输出:

----------------------------------------------先按照 channelCode 升序,再按照 rate 升序排序:OrderTest{channelCode='ALL', rate=4.3137}OrderTest{channelCode='ALL', rate=4.5137}OrderTest{channelCode='B2C', rate=5.6666}OrderTest{channelCode='Z04', rate=4.3137}OrderTest{channelCode='Z09', rate=7.6671}OrderTest{channelCode='Z09', rate=7.6677}

先按照 channelCode 升序,再按照 rate 升序排序,使用 reversed():

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

输出:

----------------------------------------------先按照 channelCode 升序,再按照 rate 升序排序,使用 reversed():OrderTest{channelCode='Z09', rate=7.6677}OrderTest{channelCode='Z09', rate=7.6671}OrderTest{channelCode='Z04', rate=4.3137}OrderTest{channelCode='B2C', rate=5.6666}OrderTest{channelCode='ALL', rate=4.5137}OrderTest{channelCode='ALL', rate=4.3137}

先按照 channelCode 升序,再按照 rate 降序排序:

        System.out.println("----------------------------------------------");        System.out.println("先按照 channelCode 升序,再按照 rate 降序排序:");        List<OrderTest> channelCodeAscRateDescList =                orderTestList.stream()                        .sorted(Comparator.comparing(OrderTest::getChannelCode)    .thenComparing(OrderTest::getRate, Comparator.reverseOrder()))                        .collect(Collectors.toList());        channelCodeAscRateDescList.forEach(System.out::println);

输出:

----------------------------------------------先按照 channelCode 升序,再按照 rate 降序排序:OrderTest{channelCode='ALL', rate=4.5137}OrderTest{channelCode='ALL', rate=4.3137}OrderTest{channelCode='B2C', rate=5.6666}OrderTest{channelCode='Z04', rate=4.3137}OrderTest{channelCode='Z09', rate=7.6677}OrderTest{channelCode='Z09', rate=7.6671}

对 null 值处理

只演示第二个字段,排序方式为倒序,用到了 Comparator.nullsLast(Comparator.reverseOrder()) 以及 Comparator.nullsLast(Comparator.reverseOrder())

添加对照数据

        orderTestList.add(z092);        orderTestList.add(B2C1);        orderTestList.add(Z041);        

先按照 channelCode 升序,再按照 rate 降序并且 null 值放前面排序:

        System.out.println("----------------------------------------------");        System.out.println("先按照 channelCode 升序,再按照 rate 降序并且 null 值放前面排序:");        List<OrderTest> channelCodeAscRateDescNullFirstList = orderTestList.stream()                .sorted(Comparator.comparing(OrderTest::getChannelCode)                        .thenComparing(OrderTest::getRate, Comparator.nullsFirst(Comparator.reverseOrder())))                .collect(Collectors.toList());        channelCodeAscRateDescNullFirstList.forEach(System.out::println);

输出:

----------------------------------------------先按照 channelCode 升序,再按照 rate 降序并且 null 值放前面排序:OrderTest{channelCode='ALL', rate=4.5137}OrderTest{channelCode='ALL', rate=4.3137}OrderTest{channelCode='B2C', rate=5.6666}OrderTest{channelCode='B2C', rate=5.6666}OrderTest{channelCode='Z04', rate=null}OrderTest{channelCode='Z04', rate=4.3137}OrderTest{channelCode='Z09', rate=null}OrderTest{channelCode='Z09', rate=7.6677}OrderTest{channelCode='Z09', rate=7.6671}

先按照 channelCode 升序,再按照 rate 降序并且 null 值放后面排序:

        System.out.println("----------------------------------------------");        System.out.println("先按照 channelCode 升序,再按照 rate 降序并且 null 值放后面排序:");        List<OrderTest> channelCodeAscRateDescNullLastList = orderTestList.stream()                .sorted(Comparator.comparing(OrderTest::getChannelCode)                        .thenComparing(OrderTest::getRate, Comparator.nullsLast(Comparator.reverseOrder())))                .collect(Collectors.toList());        channelCodeAscRateDescNullLastList.forEach(System.out::println);

输出:

----------------------------------------------先按照 channelCode 升序,再按照 rate 降序并且 null 值放后面排序:OrderTest{channelCode='ALL', rate=4.5137}OrderTest{channelCode='ALL', rate=4.3137}OrderTest{channelCode='B2C', rate=5.6666}OrderTest{channelCode='B2C', rate=5.6666}OrderTest{channelCode='Z04', rate=4.3137}OrderTest{channelCode='Z04', rate=null}OrderTest{channelCode='Z09', rate=7.6677}OrderTest{channelCode='Z09', rate=7.6671}OrderTest{channelCode='Z09', rate=null}

更多样例待补充……

来源地址:https://blog.csdn.net/MrBaymax/article/details/130723656

--结束END--

本文标题: Java 字段 Stream 排序

本文链接: https://lsjlt.com/news/417834.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作