• Java8 Comparator 排序方法


    Java8 中 Comparator 接口提供了一些静态方法,可以方便于我们进行排序操作,下面通过例子讲解下如何使用

    对整数列表排序(升序)

    List<Integer> list = Arrays.asList(1, 4, 2, 6, 2, 8);
    list.sort(Comparator.naturalOrder());
    System.out.println(list);

    对整数列表排序(降序)

    List<Integer> list = Arrays.asList(1, 4, 2, 6, 2, 8);
    list.sort(Comparator.reverseOrder());
    System.out.println(list);

    根据对象属性(年龄)进行排序

    public class Test {
        public static void main(String[] args) {
            List<Person> personList = new ArrayList<>();
            personList.add(new Person("a", 2));
            personList.add(new Person("b", 4));
            personList.add(new Person("c", 7));
            // 升序
            personList.sort(Comparator.comparingInt(Person::getAge));
            // 降序
            personList.sort(Comparator.comparingInt(Person::getAge).reversed());
            System.out.println(personList);
        }
    
        public static class Person {
            private String name;
            private Integer age;
    
            public Person(String name, Integer age) {
                this.name = name;
                this.age = age;
            }
    
            public Integer getAge() {
                return age;
            }
            
            // ... toString 方法
        }
    }

    根据对象属性(价格、速度)进行排序,需要注意的是,排序有先后之分,不同的顺序会导致不同的结果

    public class Test {
        public static void main(String[] args) {
            List<Computer> list = new ArrayList<>();
            list.add(new Computer("xiaomi",4000,6));
            list.add(new Computer("sony",5000,4));
            list.add(new Computer("dell",4000,5));
            list.add(new Computer("mac",6000,8));
            list.add(new Computer("micro",5000,6));
            // 先以价格(升序)、后再速度(升序)
       list.sort(Comparator.comparingInt(Computer::getPrice).thenComparingInt(Computer::getSpeed));
            // 先以速度(降序)、后再价格(升序)
         list.sort(Comparator.comparingInt(Computer::getSpeed).reversed().thenComparingInt(Computer::getPrice));
            // 先以价格(降序)、后再速度(降序)
            list.sort(Comparator.comparingInt(Computer::getPrice).thenComparingInt(Computer::getSpeed).reversed());
            System.out.println(list);
        }
    
        public static class Computer {
            private String name;
            private Integer price;
            private Integer speed;
    
            public Computer(String name, Integer price, Integer speed) {
                this.name = name;
                this.price = price;
                this.speed = speed;
            }
    
            public Integer getPrice() {
    
                return price;
            }
    
            public void setPrice(Integer price) {
                this.price = price;
            }
    
            public Integer getSpeed() {
                return speed;
            }
    
            public void setSpeed(Integer speed) {
                this.speed = speed;
            }
    
            // ... toString 方法
        }
    }
  • 相关阅读:
    Android ListView和CheckBox应用之批量删除操作(附带源码)
    Spring3 MVC 3到Spring MVC框架简介
    Struts2.3 + Spring3.1 + Hibernate3.6 + HTML5 + CSS3整合开发
    Java和MongoDB之Hello World
    SQL Server 2008中的hierarchyid
    SQLSERVER复制表的方法
    去掉struts2标签的样式
    KineticJS教程(12)
    SQL Server日期时间格式转换字符串详解
    生活不易,且行且珍惜!
  • 原文地址:https://www.cnblogs.com/tinya/p/12101192.html
Copyright © 2020-2023  润新知