• 用 lambda 表达式 对 List 进行排序


            List<Integer> list = new ArrayList<>();
            list.add(4);
            list.add(6);
            list.add(3);
            list.add(5);
            list.add(1);
            list.add(2);
            Iterator<Integer> iterator = list.iterator();
            System.out.println("排序前:");
            while(iterator.hasNext()) {
                System.out.print(iterator.next()+" ");
            }
            list.sort((a, b) -> a - b);       //排序
            Iterator<Integer> iteratorSort = list.iterator();
            System.out.println("
    "+"排序后:");
            while(iteratorSort.hasNext()) {
                System.out.print(iteratorSort.next()+" ");
            }

    输出结果:

    排序前:
    4 6 3 5 1 2 
    排序后:
    1 2 3 4 5 6 

    下面用实体类里面的属性进行排序:

    Person类:

    public class Person {
        
        private String name;
        
        private Integer age;
        
        private Integer score;
        
    
        public Person(String name, Integer age, Integer score) {
            super();
            this.name = name;
            this.age = age;
            this.score = score;
        }
    
        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 Integer getScore() {
            return score;
        }
    
        public void setScore(Integer score) {
            this.score = score;
        }
    
        @Override
        public String toString() {
            return "Person [name=" + name + ", age=" + age + ", score=" + score + "]";
        }
        
    
    }

    测试一下:

            List<Person> list = new ArrayList<>();
            list.add(new Person("lc",22,100));
            list.add(new Person("ly",16,70));
            list.add(new Person("yyz",20,60));
            list.add(new Person("mxy",20,80));
            list.add(new Person("lxy",15,90));
            list.sort((a, b) -> a.getAge() - b.getAge());   //按年龄排序
            System.out.println("按年龄排序:");
            Iterator<Person> iterator_age = list.iterator(); 
            while(iterator_age.hasNext()) {
                System.out.println(iterator_age.next()+" ");
            }
            
            list.sort((a, b) -> a.getScore() - b.getScore());
            System.out.println("按分数排序:");
            Iterator<Person> iterator_score = list.iterator();
            while(iterator_score.hasNext()) {
                System.out.println(iterator_score.next()+" ");
            }

    输出结果:

    按年龄排序:
    Person [name=lxy, age=15, score=90] 
    Person [name=ly, age=16, score=70] 
    Person [name=yyz, age=20, score=60] 
    Person [name=mxy, age=20, score=80] 
    Person [name=lc, age=22, score=100] 
    按分数排序:
    Person [name=yyz, age=20, score=60] 
    Person [name=ly, age=16, score=70] 
    Person [name=mxy, age=20, score=80] 
    Person [name=lxy, age=15, score=90] 
    Person [name=lc, age=22, score=100] 
  • 相关阅读:
    Gitlab + Gitlab runner + Window powershell
    python 连接SAP 代码
    【翻译】 For OData For C# play on RESTier
    SAP -熟练使用T-Code SHD0
    SAP MM- BAPI_PO_CHANGE 更新PO version 信息(version management)
    SAP PP- OPK8生产订单打印 配置Smart form.
    SAP Smart Form 无法通过程序自定义默认打印机问题解决
    SAP APO-主数据设置
    SAP APO-PP / DS
    SAP APO-供需匹配
  • 原文地址:https://www.cnblogs.com/nefu-Lc/p/10066547.html
Copyright © 2020-2023  润新知