• .sorted关键字进行比较排序


    package q;
    
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import lombok.ToString;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Comparator;
    import java.util.stream.Collectors;
    
    public class StreamTest {
        public static void main(String[] args) {
            Person person1 = new Person();
            person1.setAge(21);
            person1.setName("21");
    
            Person person2 = new Person();
            person2.setAge(19);
            person2.setName("19");
    
            Person person3 = new Person();
            person3.setAge(19);
            person3.setName("20");
    
            Person person4 = new Person();
            person4.setAge(20);
            person4.setName("20");
    
            Person person5 = new Person();
            person5.setAge(19);
            person5.setName("18");
    
            List<Person> people = new ArrayList<>();
            people.add(person1);
            people.add(person2);
            people.add(person3);
            people.add(person4);
            people.add(person5);
    
            List<Person> collect1 = people.stream().sorted((Comparator.comparing(Person::getAge))).collect(Collectors.toList());//只根据年龄排序,升序
            System.out.println(collect1);
    
            List<Person> collect2 = people.stream().sorted((Comparator.comparing(Person::getAge)).reversed()).collect(Collectors.toList());//只根据年龄排序,降序
            System.out.println(collect2);
    
            List<Person> collect3 = people.stream().sorted((Comparator.comparing(Person::getAge)).thenComparing(Comparator.comparing(Person::getName))).collect(Collectors.toList());//先根据年龄进行排序,遇见相同的,然后根据姓名进行排序,都升序
            System.out.println(collect3);
    
            List<Person> collect4 = people.stream().sorted((Comparator.comparing(Person::getAge)).thenComparing(Comparator.comparing(Person::getName)).reversed()).collect(Collectors.toList());//先根据年龄进行排序,遇见相同的,然后根据姓名进行排序,降序【都降序】
            System.out.println(collect4);
    
            List<Person> collect5 = people.stream().sorted(Comparator.comparing(Person::getAge).reversed().thenComparing(Comparator.comparing(Person::getName)).reversed()).collect(Collectors.toList());//年龄升序,姓名降序
            System.out.println(collect5);
    
            List<Person> collect6 = people.stream().sorted(Comparator.comparing(Person::getAge).reversed().thenComparing(Comparator.comparing(Person::getName))).collect(Collectors.toList());//根据年龄降序,姓名升序
            System.out.println(collect6);
        }
    }
    
    @Data
    @NoArgsConstructor
    @ToString
    class Person {
        private String name;
        private Integer age;
    }
    
  • 相关阅读:
    SSM——事务配置
    SSM——Spring+Mybtis整合(代理【mapper】开发模式)
    objective-c(五)关于代码块的使用
    objective-c(四)内存管理
    objective-c(三)类与对象的方法调用
    objective-c(二)基本数据类型介绍
    objective-c(一)关于基本数据类型打印输出方式
    Eclipse启动发生的错误:An internal error occurred during: "Initializing Java Tooling".
    单例模式
    Java 代理模式
  • 原文地址:https://www.cnblogs.com/Twittery/p/15843392.html
Copyright © 2020-2023  润新知