集合对像定义
集合对象以学生类(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()