• Jdk8新特性之排序


    Comparator提供的方法

    /**
     * @author WGR
     * @create 2020/4/13 -- 16:12
     */
    public class Person {
    
        private String name;
        private Integer age;
        private Double salary;
    
    
        public Person(String name, Integer age, Double salary) {
            super();
            this.name = name;
            this.age = age;
            this.salary = salary;
        }
    
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public Integer getAge() {
            return age;
        }
        public void setAge(Integer age) {
            this.age = age;
        }
        public Double getSalary() {
            return salary;
        }
        public void setSalary(Double salary) {
            this.salary = salary;
        }
    
        @Override
        public String toString() {
            return "Person [name=" + name + ", age=" + age + ", salary=" + salary + "]";
        }
    
    
    }

     排序实现

    /**
     * @author WGR
     * @create 2020/4/13 -- 16:14
     */
    public class _Comparator {
        List<Person> personList = new ArrayList<>();
    
        @Before
        public  void before() {
            personList.add(new Person("hepengju", 28, 20000.0));
            personList.add(new Person("lisi", 44, 40000.0));
            personList.add(new Person("wangwu", 55, 50000.0));
            personList.add(new Person("zhaoliu", 66, 60000.0));
            personList.add(new Person("zhangsan", 33, 33333.0));
            personList.add(new Person("zhangsan", 23, 30000.0));
        }
    
        //按照名字排序
        @Test
        public  void test() {
            personList.stream().sorted(Comparator.comparing(Person::getName)).forEach(System.out::println);
    //        Person [name=hepengju, age=28, salary=20000.0]
    //        Person [name=lisi, age=44, salary=40000.0]
    //        Person [name=wangwu, age=55, salary=50000.0]
    //        Person [name=zhangsan, age=33, salary=33333.0]
    //        Person [name=zhangsan, age=23, salary=30000.0]
    //        Person [name=zhaoliu, age=66, salary=60000.0]
        }
    
        //按照名字排序降序
        @Test
        public  void test4() {
            personList.stream().sorted(Comparator.comparing(Person::getName).reversed()).forEach(System.out::println);
    //        Person [name=zhaoliu, age=66, salary=60000.0]
    //        Person [name=zhangsan, age=33, salary=33333.0]
    //        Person [name=zhangsan, age=23, salary=30000.0]
    //        Person [name=wangwu, age=55, salary=50000.0]
    //        Person [name=lisi, age=44, salary=40000.0]
    //        Person [name=hepengju, age=28, salary=20000.0]
        }
    
        // 先按照名字排序, 名字一样再按照年龄排序, 年龄一样再按照薪资排序
        @Test
        public void test2(){
            personList.stream().sorted(Comparator.comparing(Person::getName).thenComparing(Person::getAge).thenComparing(Person::getSalary)).forEach(System.out::println);
    //        Person [name=hepengju, age=28, salary=20000.0]
    //        Person [name=lisi, age=44, salary=40000.0]
    //        Person [name=wangwu, age=55, salary=50000.0]
    //        Person [name=zhangsan, age=23, salary=30000.0]
    //        Person [name=zhangsan, age=33, salary=33333.0]
    //        Person [name=zhaoliu, age=66, salary=60000.0]
        }
    
        // 4. 处理所有空值问题(null都到最后)
        @Test
        public void test3(){
           // personList.add(null);
            personList.add(new Person(null, 33, 30000.0));
            personList.add(new Person("zhangsan", null, 30000.0));
            personList.add(new Person("zhangsan", 33, null));
            personList.add(new Person(null, null, null));
            personList.stream().sorted(Comparator.comparing(Person::getName,Comparator.nullsLast(Comparator.naturalOrder()))
                    .thenComparing(Person::getName,Comparator.nullsLast(Comparator.naturalOrder()))
                    .thenComparing(Person::getName,Comparator.nullsLast(Comparator.naturalOrder()))
                    ).forEach(System.out::println);
    //        Person [name=hepengju, age=28, salary=20000.0]
    //        Person [name=lisi, age=44, salary=40000.0]
    //        Person [name=wangwu, age=55, salary=50000.0]
    //        Person [name=zhangsan, age=33, salary=33333.0]
    //        Person [name=zhangsan, age=23, salary=30000.0]
    //        Person [name=zhangsan, age=null, salary=30000.0]
    //        Person [name=zhangsan, age=33, salary=null]
    //        Person [name=zhaoliu, age=66, salary=60000.0]
    //        Person [name=null, age=33, salary=30000.0]
    //        Person [name=null, age=null, salary=null]
    
        }
       //jdk8 lambda排序
    @Test
    public void test5() {
    personList.stream().sorted((p1,p2) -> p1.getName().compareTo(p2.getName()) ).forEach(System.out::println);
    // Person [name=hepengju, age=28, salary=20000.0]
    // Person [name=lisi, age=44, salary=40000.0]
    // Person [name=wangwu, age=55, salary=50000.0]
    // Person [name=zhangsan, age=33, salary=33333.0]
    // Person [name=zhangsan, age=23, salary=30000.0]
    // Person [name=zhaoliu, age=66, salary=60000.0]
    }

    @Test
    public void test6() {
    personList.stream().sorted((p1,p2) -> p1.getAge() - p2.getAge() ).forEach(System.out::println);
    // Person [name=zhangsan, age=23, salary=30000.0]
    // Person [name=hepengju, age=28, salary=20000.0]
    // Person [name=zhangsan, age=33, salary=33333.0]
    // Person [name=lisi, age=44, salary=40000.0]
    // Person [name=wangwu, age=55, salary=50000.0]
    // Person [name=zhaoliu, age=66, salary=60000.0]
    }

    }
  • 相关阅读:
    LLDB 常用的调试命令
    iOS https认证
    pod lib lint xxx.podspec 验证出错 Could not find a `ios` simulator
    LLDB 调试
    First throw call stack: 不打印方法名
    AOP
    增强现实
    2017
    iOS的主要框架介绍 (转载)
    iOS中都有什么设计模式?各个设计模式的作用 (转载)
  • 原文地址:https://www.cnblogs.com/dalianpai/p/12692934.html
Copyright © 2020-2023  润新知