zoukankan      html  css  js  c++  java
  • Java 8 Lambda排序 : Comparator example

    1. Classic Comparator example.
    
        Comparator<Developer> byName = new Comparator<Developer>() {
            @Override
            public int compare(Developer o1, Developer o2) {
                return o1.getName().compareTo(o2.getName());
            }
        };
    Copy
    2. Lambda expression equivalent.
    
        Comparator<Developer> byName = 
            (Developer o1, Developer o2)->o1.getName().compareTo(o2.getName());
    Copy
    1. Sort without Lambda
    Example to compare the Developer objects using their age. Normally, you use Collections.sort and pass an anonymous Comparator class like this :
    
    TestSorting.java
    package com.mkyong.java8;
    
    import java.math.BigDecimal;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    
    public class TestSorting {
    
        public static void main(String[] args) {
    
            List<Developer> listDevs = getDevelopers();
    
            System.out.println("Before Sort");
            for (Developer developer : listDevs) {
                System.out.println(developer);
            }
            
            //sort by age
            Collections.sort(listDevs, new Comparator<Developer>() {
                @Override
                public int compare(Developer o1, Developer o2) {
                    return o1.getAge() - o2.getAge();
                }
            });
        
            System.out.println("After Sort");
            for (Developer developer : listDevs) {
                System.out.println(developer);
            }
            
        }
    
        private static List<Developer> getDevelopers() {
    
            List<Developer> result = new ArrayList<Developer>();
    
            result.add(new Developer("mkyong", new BigDecimal("70000"), 33));
            result.add(new Developer("alvin", new BigDecimal("80000"), 20));
            result.add(new Developer("jason", new BigDecimal("100000"), 10));
            result.add(new Developer("iris", new BigDecimal("170000"), 55));
            
            return result;
    
        }
        
    }
    Copy
    Output
    
    Before Sort
    Developer [name=mkyong, salary=70000, age=33]
    Developer [name=alvin, salary=80000, age=20]
    Developer [name=jason, salary=100000, age=10]
    Developer [name=iris, salary=170000, age=55]
    
    After Sort
    Developer [name=jason, salary=100000, age=10]
    Developer [name=alvin, salary=80000, age=20]
    Developer [name=mkyong, salary=70000, age=33]
    Developer [name=iris, salary=170000, age=55]
    Copy
    When the sorting requirement is changed, you just pass in another new anonymous Comparator class :
    
        //sort by age
        Collections.sort(listDevs, new Comparator<Developer>() {
            @Override
            public int compare(Developer o1, Developer o2) {
                return o1.getAge() - o2.getAge();
            }
        });
        
        //sort by name    
        Collections.sort(listDevs, new Comparator<Developer>() {
            @Override
            public int compare(Developer o1, Developer o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });
                    
        //sort by salary
        Collections.sort(listDevs, new Comparator<Developer>() {
            @Override
            public int compare(Developer o1, Developer o2) {
                return o1.getSalary().compareTo(o2.getSalary());
            }
        });                
    Copy
    It works, but, do you think it is a bit weird to create a class just because you want to change a single line of code?
    
    2. Sort with Lambda
    In Java 8, the List interface is supports the sort method directly, no need to use Collections.sort anymore.
    
            //List.sort() since Java 8
        listDevs.sort(new Comparator<Developer>() {
            @Override
            public int compare(Developer o1, Developer o2) {
                return o2.getAge() - o1.getAge();
            }
        });    
    Copy
    Lambda expression example :
    
    TestSorting.java
    package com.mkyong.java8;
    
    import java.math.BigDecimal;
    import java.util.ArrayList;
    import java.util.List;
    
    public class TestSorting {
    
        public static void main(String[] args) {
    
            List<Developer> listDevs = getDevelopers();
            
            System.out.println("Before Sort");
            for (Developer developer : listDevs) {
                System.out.println(developer);
            }
            
            System.out.println("After Sort");
            
            //lambda here!
            listDevs.sort((Developer o1, Developer o2)->o1.getAge()-o2.getAge());
        
            //java 8 only, lambda also, to print the List
            listDevs.forEach((developer)->System.out.println(developer));
        }
    
        private static List<Developer> getDevelopers() {
    
            List<Developer> result = new ArrayList<Developer>();
    
            result.add(new Developer("mkyong", new BigDecimal("70000"), 33));
            result.add(new Developer("alvin", new BigDecimal("80000"), 20));
            result.add(new Developer("jason", new BigDecimal("100000"), 10));
            result.add(new Developer("iris", new BigDecimal("170000"), 55));
            
            return result;
    
        }
        
    }
    Copy
    Output
    
    Before Sort
    Developer [name=mkyong, salary=70000, age=33]
    Developer [name=alvin, salary=80000, age=20]
    Developer [name=jason, salary=100000, age=10]
    Developer [name=iris, salary=170000, age=55]
    
    After Sort
    Developer [name=jason, salary=100000, age=10]
    Developer [name=alvin, salary=80000, age=20]
    Developer [name=mkyong, salary=70000, age=33]
    Developer [name=iris, salary=170000, age=55]
    Copy
    3. More Lambda Examples
    3.1 Sort By age
    
        //sort by age
        Collections.sort(listDevs, new Comparator<Developer>() {
            @Override
            public int compare(Developer o1, Developer o2) {
                return o1.getAge() - o2.getAge();
            }
        });
        
        //lambda
        listDevs.sort((Developer o1, Developer o2)->o1.getAge()-o2.getAge());
        
        //lambda, valid, parameter type is optional
        listDevs.sort((o1, o2)->o1.getAge()-o2.getAge());
    Copy
    3.2 Sort by name
    
        //sort by name
        Collections.sort(listDevs, new Comparator<Developer>() {
            @Override
            public int compare(Developer o1, Developer o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });
            
        //lambda
        listDevs.sort((Developer o1, Developer o2)->o1.getName().compareTo(o2.getName()));        
        
        //lambda
        listDevs.sort((o1, o2)->o1.getName().compareTo(o2.getName()));        
    Copy
    3.3 Sort by salary
    
        //sort by salary
        Collections.sort(listDevs, new Comparator<Developer>() {
            @Override
            public int compare(Developer o1, Developer o2) {
                return o1.getSalary().compareTo(o2.getSalary());
            }
        });                
    
        //lambda
        listDevs.sort((Developer o1, Developer o2)->o1.getSalary().compareTo(o2.getSalary()));
        
        //lambda
        listDevs.sort((o1, o2)->o1.getSalary().compareTo(o2.getSalary()));
    Copy
    3.4 Reversed sorting.
    
    3.4.1 Lambda expression to sort a List using their salary.
    
        Comparator<Developer> salaryComparator = (o1, o2)->o1.getSalary().compareTo(o2.getSalary());
        listDevs.sort(salaryComparator);
    Copy
    Output
    
    Developer [name=mkyong, salary=70000, age=33]
    Developer [name=alvin, salary=80000, age=20]
    Developer [name=jason, salary=100000, age=10]
    Developer [name=iris, salary=170000, age=55]
    Copy
    3.4.2 Lambda expression to sort a List using their salary, reversed order.
    
        Comparator<Developer> salaryComparator = (o1, o2)->o1.getSalary().compareTo(o2.getSalary());
        listDevs.sort(salaryComparator.reversed());
    Copy
    Output
    
    Developer [name=iris, salary=170000, age=55]
    Developer [name=jason, salary=100000, age=10]
    Developer [name=alvin, salary=80000, age=20]
    Developer [name=mkyong, salary=70000, age=33]

    http://www.mkyong.com/java8/java-8-lambda-comparator-example/

  • 相关阅读:
    Jenkins权限控制-Role Strategy Plugin插件使用
    迁移一个仓库到新的Gitlab
    Gitlab备份以及恢复
    10.使用nexus3配置golang私有仓库
    9.使用nexus3配置Python私有仓库
    8.maven上传jar包以及SNAPSHOT的一个坑
    7.nexus版本升级
    6.使用nexus3配置yum私有仓库
    5.使用nexus3配置npm私有仓库
    4.maven私服nexus2迁移到nexus3
  • 原文地址:https://www.cnblogs.com/shy1766IT/p/10001055.html
Copyright © 2011-2022 走看看