有学有练才叫学习:学而不思则罔,思而不学则殆:学而不习,纸上谈兵,习而不进,画地为牢!

Java Stream.sorted 用法详解

java 炮渣日记 3周前 (11-14) 21次浏览 已收录 0个评论 扫描二维码

JDK8之前如果需要对集合排序需要实现Comparable接口的compareTo方法

如下面的代码:

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class Student implements Comparable<Student>{
    private Integer no;
    private String name;
    private String classNo;
    private int core;

    @Override
    public int compareTo(Student o) {
        return this.core-o.core;
    }
}

然后我们可以在集合中用如下方法对集合进行排序

Collections.sort(studentList);

JDK8之后提供Stream流式编程大大丰富了操作集合的方式,下面我们看一下JDK8之后排序写法。

首先载入Maven依赖,主要是引入这两个工具类方便打印信息

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.20</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.2.5</version>
</dependency>

添加Model类

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class Teacher implements Comparable<Teacher>{
    private Integer no;
    private String sex;
    private String name;
    private String classNo;
    private int core;
    //JDK8之前排序写法
    @Override
    public int compareTo(Teacher o) {
        return this.core-o.core;
    }
}

写测试类

import lombok.extern.slf4j.Slf4j;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
public class TestStreamSorted {
    public static void main(String [] args){
        List<Teacher> teacherList=new ArrayList<>();
        teacherList.addAll(Arrays.asList(
                 new Teacher(1,"男","111","12",30)
                ,new Teacher(4,"女","111","12",60)
                ,new Teacher(2,"男","111","12",40)
                ,new Teacher(6,"女","333","33",80)
                ,new Teacher(3,"女","111","12",50)
                ,new Teacher(5,"男","333","33",70)
                ,new Teacher(7,"女","333","44",90)
                ));
        Collections.sort(teacherList);
        printInfo(teacherList);
        //升序
        List <Teacher> sortedAsc=teacherList.stream().sorted(Comparator.comparing(Teacher::getNo)).collect(Collectors.toList());
        printInfo(sortedAsc);
        //降序
        List <Teacher> sortedDesc=teacherList.stream().sorted(Comparator.comparing(Teacher::getNo).reversed()).collect(Collectors.toList());
        printInfo(sortedDesc);
        teacherList.add(new Teacher(null,"女","4444","55",99));
        //排序字段NULL处理 NULL 放在最前
        List <Teacher> sortedNullFirst=teacherList.stream().sorted(Comparator.comparing(Teacher::getNo,Comparator.nullsFirst(Integer::compare))).collect(Collectors.toList());
        printInfo(sortedNullFirst);
        //排序字段NULL处理 NULL 放在最前
        List <Teacher> sortedNullLast=teacherList.stream().sorted(Comparator.comparing(Teacher::getNo,Comparator.nullsLast(Integer::compare))).collect(Collectors.toList());
        printInfo(sortedNullLast);
        //JDK8 之前用法
        Collections.sort(teacherList);
        printInfo(teacherList);
    }
    //打印信息
    private  static void printInfo(List<Teacher> teacherList) {
       teacherList.forEach(key-> {
            log.info("{}-{}", key.getNo(), key.getCore());
        });
    }

}

看一下是不是方便许多,Stream的用法还有很多,今天暂时写这么多。

欢迎大家交流,如果错误敬请指正,感谢

喜欢 (0)
炮渣日记
关于作者:
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址