zoukankan      html  css  js  c++  java
  • 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]
    }

    }
  • 相关阅读:
    FluorineFx ASObject自动转换基础类 AutoParseASObject ,用于Flash AMF协议解析
    小东西WinForm的等待窗口
    被VB6搞死。。。。。。。鸟
    请把这个消息提示框拿掉,谢谢
    MS新版Wallop,
    SQL 2000 异数据库数据同步
    企业管理应用平台预览演示版下载
    Microsoft SQL Server 2008 基本安装说明
    怀旧下给自己留个备份,
    PPPOE数据包转换及SharpPcap应用
  • 原文地址:https://www.cnblogs.com/dalianpai/p/12692934.html
Copyright © 2011-2022 走看看