java8 Stream 多个条件排序

文章讲述了如何使用Java8的Stream和Comparator对一个List>进行排序,首先按idle键对应的日期升序,日期相同则按person键对应的Person对象的年龄升序。示例代码展示了排序过程和排序后的结果。
摘要由CSDN通过智能技术生成

话不多说,直接上需求,一个简简单单的需求

    @Data
    @Accessors(chain = true)
    static
    class Person {
        private String name;
        private Integer age;
    }
    public static void main(String[] args) {
        //
        List<Map<String, Object>> list = new ArrayList<>();

        Map<String, Object> map1 = new HashMap<>();
        map1.put("idle", "2022-03-03");
        map1.put("person", new Person().setName("test1").setAge(30));

        Map<String, Object> map2 = new HashMap<>();
        map2.put("idle", "2024-04-04");
        map2.put("person", new Person().setName("test2").setAge(50));

        Map<String, Object> map3 = new HashMap<>();
        map3.put("idle", "2023-04-04");
        map3.put("person", new Person().setName("test3").setAge(60));

        Map<String, Object> map4 = new HashMap<>();
        map4.put("idle", "2023-04-04");
        map4.put("person", new Person().setName("test4").setAge(50));

        Map<String, Object> map5 = new HashMap<>();
        map5.put("idle", "2023-04-04");
        map5.put("person", new Person().setName("test5").setAge(55));

        list.add(map1);
        list.add(map2);
        list.add(map3);
        list.add(map4);
        list.add(map5);

        list.forEach(l -> log.info("{}", l));
    }

现在我想将list继续排序,要求是先按照idle升序,如果idle一样,按照Person的age升序,如果没进行排序,上面代码main方法执行后结果如下:

{idle=2022-03-03, person=MyTest.Person(name=test1, age=30)}
{idle=2024-04-04, person=MyTest.Person(name=test2, age=50)}
{idle=2023-04-04, person=MyTest.Person(name=test3, age=60)}
{idle=2023-04-04, person=MyTest.Person(name=test4, age=50)}
{idle=2023-04-04, person=MyTest.Person(name=test5, age=55)}

现在进行排序,在main方法中继续追加如下代码:

        log.info("=============================================== 开始排序, idele升序, idle相同的age升序");
        list = list.stream().sorted(
        		// 使用(Function<Map<String, Object>, String>)强转的原因是类型转换失败问题
                Comparator.comparing((Function<Map<String, Object>, String>) map -> (String) map.get("idle"))
                        .thenComparing(map -> {
                            final Person person = (Person) map.get("person");
                            return person.getAge();
                        })
        ).collect(Collectors.toList());

        list.forEach(l -> log.info("{}", l));

再次执行main方法:

{idle=2022-03-03, person=MyTest.Person(name=test1, age=30)}
{idle=2024-04-04, person=MyTest.Person(name=test2, age=50)}
{idle=2023-04-04, person=MyTest.Person(name=test3, age=60)}
{idle=2023-04-04, person=MyTest.Person(name=test4, age=50)}
{idle=2023-04-04, person=MyTest.Person(name=test5, age=55)}
=============================================== 开始排序, idele升序, idle相同的age升序
{idle=2022-03-03, person=MyTest.Person(name=test1, age=30)}
{idle=2023-04-04, person=MyTest.Person(name=test4, age=50)}
{idle=2023-04-04, person=MyTest.Person(name=test5, age=55)}
{idle=2023-04-04, person=MyTest.Person(name=test3, age=60)}
{idle=2024-04-04, person=MyTest.Person(name=test2, age=50)}

现在可以看到,结果是我们想要的了

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值