当前位置:   article > 正文

Java8 Lambda.stream.sorted() 方法使用浅析分享_java8 sorted

java8 sorted

Java8 Lambda.stream.sorted() 方法使用浅析分享

本文主要分享运用 Java8 中的 Lambda.stream.sorted方法排序的使用!

sorted() 重载方法一

sorted():默认自然排序;

升序

 @Test
    public void testSorted1() {
        List<Integer> list = Lists.newArrayList(2,5,3,4,1,2,6,7,9,1);
        List<Integer> collect = list.stream().sorted().collect(Collectors.toList());
        System.out.println(JSONObject.toJSONString(collect));
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

运行结果:

在这里插入图片描述

降序

倒序需要结合 Comparator.reverseOrder() 方法使用:

 @Test
    public void testSorted1() {
        List<Integer> list = Lists.newArrayList(2,5,3,4,1,2,6,7,9,1);
        List<Integer> collect = list.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
        System.out.println(JSONObject.toJSONString(collect));
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

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

sorted() 重载方法二

sorted(Comparator<? super T> comparator):通过创建 Comparator 实例,按照指定规则升/降序排序元素。

升序

按生日升序:

    @Test
    public void testSorted2() {
        List<Student> list = this.getStudent();
        List<Student> collect = list.stream().sorted(Comparator.comparing(Student::getBirthday))
                .collect(Collectors.toList());
        System.out.println(JSONObject.toJSONString(collect));
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

降序

按生日降序:

	@Test
    public void testSorted2() {
        List<Student> list = this.getStudent();
        List<Student> collect = list.stream().sorted(Comparator.comparing(Student::getBirthday, Comparator.reverseOrder()))
                .collect(Collectors.toList());
        System.out.println(JSONObject.toJSONString(collect));
    }	
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

多字段排序

排序说明:

  1. 生日升序;

  2. 学号降序;

 	@Test
    public void testSorted2() {
        List<Student> list = this.getStudent();
        List<Student> collect = list.stream().sorted(Comparator.comparing(Student::getBirthday)
                        .thenComparing(Student::getNum, Comparator.reverseOrder()))
                .collect(Collectors.toList());
        System.out.println(JSONObject.toJSONString(collect));
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

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

mock代码

student对象:

@AllArgsConstructor
@NoArgsConstructor
@Data
public class Student {

    /**
     * 姓名
     */
    private String name;
    /**
     * 年龄
     */
    private Integer age;
    /**
     * 生日
     */
    @JSONField(format="yyyy-MM-dd HH:mm:ss")
    private Date birthday;
    /**
     * 学号
     */
    private Integer num;

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

mock数据

public List<Student> getStudent() {
        return Lists.newArrayList(
                new Student("小张", 17, DateUtil.parse("2006-10-03 15:18:56"), 11),
                new Student("小李", 15, DateUtil.parse("2008-03-19 02:18:56"), 5),
                new Student("小李", 15, DateUtil.parse("2008-03-19 02:18:56"), 2),
                new Student("小王", 16, DateUtil.parse("2007-02-21 22:18:56"), 29));
    }
eUtil.parse("2008-03-19 02:18:56"), 2),
                new Student("小王", 16, DateUtil.parse("2007-02-21 22:18:56"), 29));
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

感 谢 各 位 大 佬 的 阅 读,随 手 点 赞,日 薪 过 万~! !!
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/754603
推荐阅读
相关标签
  

闽ICP备14008679号