Java8 使用 stream().sorted()對List集合進行排序

集合對像定義
集合對象以學(xué)生類(StudentInfo)為例,有學(xué)生的基本信息,包括:姓名,性別,年齡,身高,生日幾項。

使用stream().sorted()進行排序,需要該類實現(xiàn) Comparable 接口,該接口只有一個方法需要實現(xiàn),如下:

public int compareTo(T o);

有關(guān)compareTo方法的實現(xiàn)說明,請參考:Java 關(guān)于重寫compareTo方法

我的學(xué)生類代碼如下:StudentInfo對象類

import java.time.LocalDate;
import java.util.List;

public class StudentInfo implements Comparable<StudentInfo> {

    //名稱
    private String name;

    //性別 true男 false女
    private Boolean gender;

    //年齡
    private Integer age;

    //身高
    private Double height;

    //出生日期
    private LocalDate birthday;

    public StudentInfo(String name, Boolean gender, Integer age, Double height, LocalDate birthday){
        this.name = name;
        this.gender = gender;
        this.age = age;
        this.height = height;
        this.birthday = birthday;
    }

    public String toString(){
        String info = String.format("%s\t\t%s\t\t%s\t\t\t%s\t\t%s",this.name,this.gender.toString(),this.age.toString(),this.height.toString(),birthday.toString());
        return info;
    }

    public static void printStudents(List<StudentInfo> studentInfos){
        System.out.println("[姓名]\t\t[性別]\t\t[年齡]\t\t[身高]\t\t[生日]");
        System.out.println("----------------------------------------------------------");
        studentInfos.forEach(s->System.out.println(s.toString()));
        System.out.println(" ");
    }

    @Override
    public int compareTo(StudentInfo ob) {
        return this.age.compareTo(ob.getAge());
        //return 1;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Boolean getGender() {
        return gender;
    }

    public void setGender(Boolean gender) {
        this.gender = gender;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Double getHeight() {
        return height;
    }

    public void setHeight(Double height) {
        this.height = height;
    }

    public LocalDate getBirthday() {
        return birthday;
    }

    public void setBirthday(LocalDate birthday) {
        this.birthday = birthday;
    }
}

添加測試數(shù)據(jù)

下面來添加一些測試用的數(shù)據(jù),代碼如下:

//測試數(shù)據(jù),請不要糾結(jié)數(shù)據(jù)的嚴(yán)謹(jǐn)性
List<StudentInfo> studentList = new ArrayList<>();
studentList.add(new StudentInfo("李小明",true,18,1.76,LocalDate.of(2001,3,23)));
studentList.add(new StudentInfo("張小麗",false,18,1.61,LocalDate.of(2001,6,3)));
studentList.add(new StudentInfo("王大朋",true,19,1.82,LocalDate.of(2000,3,11)));
studentList.add(new StudentInfo("陳小跑",false,17,1.67,LocalDate.of(2002,10,18)));

排序

使用年齡進行升序排序

//排序前輸出
StudentInfo.printStudents(studentList);
//按年齡排序(Integer類型)
List<StudentInfo> studentsSortName = studentList.stream().sorted(Comparator.comparing(StudentInfo::getAge)).collect(Collectors.toList());
//排序后輸出
StudentInfo.printStudents(studentsSortName);

結(jié)果如下圖:


使用年齡進行降序排序(使用reversed()方法)

//排序前輸出
StudentInfo.printStudents(studentList);
//按年齡排序(Integer類型)
List<StudentInfo> studentsSortName = studentList.stream().sorted(Comparator.comparing(StudentInfo::getAge).reversed()).collect(Collectors.toList());
//排序后輸出
StudentInfo.printStudents(studentsSortName);

結(jié)果如下圖:


使用年齡進行降序排序,年齡相同再使用身高升序排序

//排序前輸出
        StudentInfo.printStudents(studentList);
        //按年齡排序(Integer類型)
        List<StudentInfo> studentsSortName = studentList.stream()
                .sorted(Comparator.comparing(StudentInfo::getAge).reversed().thenComparing(StudentInfo::getHeight))
                .collect(Collectors.toList());
        //排序后輸出
        StudentInfo.printStudents(studentsSortName);

結(jié)果如下圖:


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容