java8排序

@Data
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;
    }

    @Override
    public String toString() {
        return "StudentInfo{" +
                "name='" + name + '\'' +
                ", gender=" + gender +
                ", age=" + age +
                ", height=" + height +
                ", birthday=" + birthday +
                '}';
    }

    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 o) {
        return this.age.compareTo(o.getAge());
    }

    public String getName() {
        return name;
    }

    public Boolean getGender() {
        return gender;
    }

    public Integer getAge() {
        return age;
    }

    public Double getHeight() {
        return height;
    }
=================================================================================================================


public class sort {
    public static void main(String[] args) {
        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);

        //final List<StudentInfo> studentsSortName  = studentList.stream().sorted(Comparator.comparing(StudentInfo::getAge)).collect(Collectors.toList());
        final List<StudentInfo> studentsSortName = studentList.stream()
                .sorted(Comparator.comparing(StudentInfo::getAge).reversed()
                        .thenComparing(StudentInfo::getHeight))
                .collect(Collectors.toList());

        StudentInfo.printStudents(studentsSortName);
    }

}

运行结果

[姓名]        [性别]        [年龄]        [身高]        [生日]
----------------------------------------------------------
StudentInfo{name='李小明', gender=true, age=18, height=1.76, birthday=2001-03-23}
StudentInfo{name='张小丽', gender=false, age=18, height=1.61, birthday=2001-06-03}
StudentInfo{name='王大朋', gender=true, age=19, height=1.82, birthday=2000-03-11}
StudentInfo{name='陈小跑', gender=false, age=17, height=1.67, birthday=2002-10-18}
 
[姓名]        [性别]        [年龄]        [身高]        [生日]
----------------------------------------------------------
StudentInfo{name='王大朋', gender=true, age=19, height=1.82, birthday=2000-03-11}
StudentInfo{name='张小丽', gender=false, age=18, height=1.61, birthday=2001-06-03}
StudentInfo{name='李小明', gender=true, age=18, height=1.76, birthday=2001-03-23}
StudentInfo{name='陈小跑', gender=false, age=17, height=1.67, birthday=2002-10-18}

 

 

如果年龄相同按照身高大的排在前面thenComparing默认从小到大排

public static void main(String[] args) {
    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);

    Function<StudentInfo, Integer> function = p -> -p.getHeight().compareTo(p.getHeight());

    //final List<StudentInfo> studentsSortName  = studentList.stream().sorted(Comparator.comparing(StudentInfo::getAge)).collect(Collectors.toList());
    final List<StudentInfo> studentsSortName = studentList.stream()
            .sorted(Comparator.comparing(StudentInfo::getAge).reversed()
                    .thenComparing((p,p1)-> p1.getHeight().compareTo(p.getHeight())))
            .collect(Collectors.toList());

    StudentInfo.printStudents(studentsSortName);
}

运行结果

[姓名]        [性别]        [年龄]        [身高]        [生日]
----------------------------------------------------------
StudentInfo{name='李小明', gender=true, age=18, height=1.76, birthday=2001-03-23}
StudentInfo{name='张小丽', gender=false, age=18, height=1.61, birthday=2001-06-03}
StudentInfo{name='王大朋', gender=true, age=19, height=1.82, birthday=2000-03-11}
StudentInfo{name='陈小跑', gender=false, age=17, height=1.67, birthday=2002-10-18}
 
[姓名]        [性别]        [年龄]        [身高]        [生日]
----------------------------------------------------------
StudentInfo{name='王大朋', gender=true, age=19, height=1.82, birthday=2000-03-11}
StudentInfo{name='李小明', gender=true, age=18, height=1.76, birthday=2001-03-23}
StudentInfo{name='张小丽', gender=false, age=18, height=1.61, birthday=2001-06-03}
StudentInfo{name='陈小跑', gender=false, age=17, height=1.67, birthday=2002-10-18}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值