Comparator

package com.easypoi.stu.oneDay.collection;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class ComparatorTest {
    static Student s1 = new Student("Ram", 18);
    static Student s2 = new Student("Shyam", 22);
    static Student s3 = new Student("Mohan", 17);

    public static void main(String[] args) {
        thenComparing();
    }

    private static void thenComparing() {
        List<Student2> studentList = Student2.getStudentList();
        // 先根据年龄进行排序,如果有年龄相同的再跟进姓名排序
        Comparator<Student2> comparator = Comparator.comparing(Student2::getAge)
                .thenComparing(Student2::getName);

        Collections.sort(studentList, comparator);
        studentList.forEach(s -> {
            System.out.println(s.getAge() + "-" + s.getName());
        });

        System.out.println("\r\n---------------");
        // 先根据school的sname进行排序,如果sname相同再根据code排序。如果school的sname和code都一样再根据学生age排序,如果还是相同再根据学生name进行排序
        Comparator<Student2> comparator1 = Comparator.comparing(Student2::getSchool)
                .thenComparing(Student2::getSchool, Comparator.comparing(School::getCode))
                .thenComparing(Student2::getAge)
                .thenComparing(Student2::getName);
        Collections.sort(studentList, comparator1);
        studentList.forEach(s -> System.out.println(s.getSchool().getSname() + "|" + s.getSchool().getCode() + "|" + s.getAge() + "|" + s.getName()));
    }

    private static void comparingIntLongDouble() {
        List<Student2> studentList = Student2.getStudentList();
        Collections.sort(studentList, Comparator.comparingInt(Student2::getAge));

        Collections.sort(studentList, Comparator.comparingLong(Student2::getHomeDistance));

        Collections.sort(studentList, Comparator.comparingDouble(Student2::getWeight));
    }

    private static void comparing() {
        List<Student2> studentList = Student2.getStudentList();
        // 这里school的比较,是根据它从写的compareTo来比较的,也就是比较的是sname
        Comparator<Student2> schoolComparator1 = Comparator.comparing(Student2::getSchool);
        Collections.sort(studentList, schoolComparator1);
        studentList.forEach(s -> System.out.print(s.getName() + "-" + s.getSchool().getSname() + " | "));
        System.out.println("\n-------------------");

        // 这里根据school来排序。但是比较school的时候指定了comparator,比较的是code而不是sname了
        Comparator<Student2> schoolComparator2 =
                Comparator.comparing(Student2::getSchool, (sch1, sch2) -> sch1.getCode() - sch2.getCode());
        Collections.sort(studentList, schoolComparator2);
        studentList.forEach(s -> System.out.print(s.getName() + "-" + s.getSchool().getCode() + " | "));
        System.out.println("\n-------------------");
    }


    private static void nullAtLast() {
        System.out.println("-------Case1: One null----------");

        List<Student> list = Arrays.asList(s1, s2, null, s3);
        Collections.sort(list, Comparator.nullsLast(Comparator.comparing(Student::getName)));
        list.forEach(s -> System.out.println(s));

        System.out.println("--------Case2: More than one null---------");

        list = Arrays.asList(s1, null, s2, null, s3);
        Collections.sort(list, Comparator.nullsLast(Comparator.comparing(Student::getName)));
        list.forEach(s -> System.out.println(s));
    }

    private static void nullFirst() {
        System.out.println("-------Case1: One null----------");

        List<Student> list = Arrays.asList(s1, s2, null, s3);
        Collections.sort(list, Comparator.nullsFirst(Comparator.comparing(Student::getName)));
        list.forEach(s -> System.out.println(s));

        System.out.println("--------Case2: More than one null---------");

        list = Arrays.asList(s1, null, s2, null, s3);
        Collections.sort(list, Comparator.nullsFirst(Comparator.comparing(Student::getName)));
        list.forEach(s -> System.out.println(s));

        System.out.println("--------Case5: Specify natural order Comparator to nullsFirst---------");

        list = Arrays.asList(s1, null, s2, null, s3);
        Collections.sort(list, Comparator.nullsFirst(Comparator.naturalOrder()));
        list.forEach(s -> System.out.println(s));
    }

    private static void naturalOrder() {
        List<Integer> numList = Arrays.asList(12, 10, 15, 8, 11);
        // 自然顺序
        numList.sort(Comparator.naturalOrder());
        numList.forEach(n -> System.out.print(n + " "));
        System.out.println("\n-----------");
        Collections.sort(numList, Comparator.reverseOrder());
        numList.forEach(n -> System.out.print(n + " "));
    }

    private static void test1() {
        List<Student> studentList = Student.getStudentList();
        System.out.println("--- Sort Students by age ---");
        // Comparator<Student> comparing = Comparator.comparing(Student::getAge);
        Comparator<Student> ageCom = (s1, s2) -> s1.getAge() - s2.getAge();
        studentList.sort(ageCom);
        studentList.forEach(s -> System.out.println(s));

        System.out.println("--- Sort Students by name ---");
        Comparator<Student> nameCom = (s1, s2) -> s1.getName().compareTo(s2.getName());
        studentList.sort(nameCom);
        studentList.forEach(s -> System.out.println(s));

        System.out.println("--- Reversed Sort Students by age ---");
        Comparator<Student> reversed = ageCom.reversed();
        studentList.sort(reversed);
        studentList.forEach(System.out::println);
    }

    static class Student implements Comparable<Student> {
        private String name;
        private int age;

        public Student(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public int getAge() {
            return age;
        }

        @Override
        public int compareTo(Student s) {
            return name.compareTo(s.getName());
        }

        @Override
        public String toString() {
            return name + "-" + age;
        }

        public static List<Student> getStudentList() {
            Student s1 = new Student("Ram", 18);
            Student s2 = new Student("Shyam", 22);
            Student s3 = new Student("Mohan", 19);
            List<Student> list = Arrays.asList(s1, s2, s3);
            return list;
        }
    }

    static public class School implements Comparable<School> {
        private int code;
        private String sname;

        public School(int code, String sname) {
            this.code = code;
            this.sname = sname;
        }

        public int getCode() {
            return code;
        }

        public String getSname() {
            return sname;
        }

        @Override
        public int compareTo(School s) {
            return s.sname.compareTo(sname);
        }
    }

    static public class Student2 {
        private String name;
        private int age;
        private long homeDistance;
        private double weight;
        private School school;

        public Student2(String name, int age, long homeDistance, double weight, School school) {
            this.name = name;
            this.age = age;
            this.homeDistance = homeDistance;
            this.weight = weight;
            this.school = school;
        }

        public String getName() {
            return name;
        }

        public int getAge() {
            return age;
        }

        public long getHomeDistance() {
            return homeDistance;
        }

        public double getWeight() {
            return weight;
        }

        public School getSchool() {
            return school;
        }

        public static List<Student2> getStudentList() {
            Student2 s1 = new Student2("Ram", 18, 3455, 60.75, new School(101, "PQ College"));
            Student2 s2 = new Student2("Shyam", 22, 3252, 65.80, new School(103, "RS College"));
            Student2 s3 = new Student2("Mohan", 19, 1459, 65.20, new School(102, "AB College"));
            Student2 s4 = new Student2("Mahesh", 20, 4450, 70.25, new School(104, "CD College"));

            Student2 s5 = new Student2("Aam", 18, 3455, 60.75, new School(101, "PQ College"));
            List<Student2> list = Arrays.asList(s1, s2, s3, s4, s5);
            return list;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hello_中年人

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值