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


集合對像定義

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

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

public int compareTo(T o);

有關compareTo方法的實現說明,請參考:Java 關於重寫compareTo方法

我的學生類代碼如下:

復制代碼
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;
}
}

復制代碼

添加測試數據

下面來添加一些測試用的數據,代碼如下:

復制代碼
//測試數據,請不要糾結數據的嚴謹性
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);
復制代碼

結果如下圖:

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

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

結果如下圖:

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

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

結果如下圖:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM