美文网首页
集合的优雅排序:stream、Comparator

集合的优雅排序:stream、Comparator

作者: 综合楼 | 来源:发表于2023-11-30 14:08 被阅读0次

Comparator

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

public class TestComparator {
 
    public static void main(String[] args) {
        List<Student> students = buildStudents();

        // 使用lambda表达式创建Function对象
        Function<Student, Integer> extractIdWay1 = (student) -> student.getId();

        // 使用方法引用简化lambda
        Function<Student, Integer> extractIdWay2 = Student::getId;

        // Comparator.comparing(Function keyExtractor)
        Comparator<Student> byId = Comparator.comparing(extractIdWay2);

        // 升序
        List<Student> ascList = students.stream().sorted(byId).collect(Collectors.toList());
        System.out.println(ascList);

        // 降序
        List<Student> descList = students.stream().sorted(byId.reversed()).collect(Collectors.toList());
        System.out.println(descList);

        //如果出现多个对象的组合封装需要排序,可以层层构造comparator
        Comparator<Address> cmpAddr = Comparator.comparing(Address::getAddress);
        Comparator<Student> byAddress = Comparator.comparing(Student::getAddress, cmpAddr);
        List<Student> sortedAddressList = students.stream().sorted(byAddress).collect(Collectors.toList());
        System.out.println(sortedAddressList);

        // 包含null
        Comparator<Student> nullNotAllowed = Comparator.comparing(Student::getId);
        Comparator<Student> allowNullComparator = Comparator.nullsFirst(nullNotAllowed);

        // 正常排序
        List<Student> result1 = students.stream().sorted(allowNullComparator).collect(Collectors.toList());
        System.out.println(result1);

        // 抛异常
        List<Student> result2 = students.stream().sorted(nullNotAllowed).collect(Collectors.toList());
        System.out.println(result2);


    }
 
    private static List<Student> buildStudents() {
        List<Student> students = new ArrayList<>();
        students.add(new Student(10, 20, "aty", new Address("d")));
        students.add(new Student(1, 22, "qun", new Address("c")));
        students.add(new Student(1, 26, "Zen", new Address("b")));
        students.add(new Student(5, 23, "aty", new Address("a")));
        return students;
    }
 
}

Comparable

@Data
public class StudentComparable implements Comparable<StudentComparable> {
    private int id;
    private Integer age;
    private String name;
    private Address address;

    public StudentComparable(int id, Integer age, String name, Address address) {
        this.id = id;
        this.age = age;
        this.name = name;
        this.address = address;
    }

    @Override
    public int compareTo(StudentComparable o) {
        return this.getId() - o.getId();
    }
}

public class TestComparable {
 
    public static void main(String[] args) {
        List<StudentComparable> students = buildStudents();
        
        // 按照默认顺序排序
        List<StudentComparable> ascList1 = students.stream().sorted().collect(Collectors.toList());
        System.out.println(ascList1);

        // 按照自然序排序(其实就是默认顺序)
        List<StudentComparable> ascList2 = students.stream().sorted(Comparator.naturalOrder()).collect(Collectors.toList());
        System.out.println(ascList2);

        // 按照默认顺序的相反顺序排序
        List<StudentComparable> descList = students.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
        System.out.println(descList);
 
    }
 
    private static List<StudentComparable> buildStudents() {
        List<StudentComparable> students = new ArrayList<>();
        students.add(new StudentComparable(10, 20, "aty", new Address("d")));
        students.add(new StudentComparable(1, 22, "qun", new Address("c")));
        students.add(new StudentComparable(1, 26, "Zen", new Address("b")));
        students.add(new StudentComparable(5, 23, "aty", new Address("a")));
        return students;
    }
 
}

相关文章

网友评论

      本文标题:集合的优雅排序:stream、Comparator

      本文链接:https://www.haomeiwen.com/subject/cdxxgdtx.html