集合對像定義
集合對象以學生類(StudentInfo)爲例,有學生的基本信息,包括:姓名,性別,年齡,身高,生日幾項。java
使用stream().sorted()進行排序,須要該類實現 Comparable 接口,該接口只有一個方法須要實現,以下:ide
public int compareTo(T o);
有關compareTo方法的實現說明,請參考:Java 關於重寫compareTo方法測試
個人學生類代碼以下:this
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; } }
添加測試數據url
下面來添加一些測試用的數據,代碼以下:spa
//測試數據,請不要糾結數據的嚴謹性 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);
結果以下圖:.net
使用年齡進行降序排序(使用reversed()方法)
//排序前輸出 StudentInfo.printStudents(studentList); //按年齡排序(Integer類型) List<StudentInfo> studentsSortName = studentList.stream().sorted(Comparator.comparing(StudentInfo::getAge).reversed()).collect(Collectors.toList()); //排序後輸出 StudentInfo.printStudents(studentsSortName);
結果以下圖:code
使用年齡進行降序排序,年齡相同再使用身高升序排序
//排序前輸出 StudentInfo.printStudents(studentList); //按年齡排序(Integer類型) List<StudentInfo> studentsSortName = studentList.stream() .sorted(Comparator.comparing(StudentInfo::getAge).reversed().thenComparing(StudentInfo::getHeight)) .collect(Collectors.toList()); //排序後輸出 StudentInfo.printStudents(studentsSortName);
結果以下圖:orm