Java 8 Lamba表达式使用比较器

时间:2020-02-23 14:34:53  来源:igfitidea点击:

Java 8使用Lambda表达式更强大的比较器。

它为比较器推出了许多新的API。

让我们通过Lambda表达式来通过比较器的一些新功能.Let的创建一个称为员工的非常简单的类:

package org.igi.theitroad;
 
public class Employee {
 
 private String employeeName;
 private int age;
 
 public Employee(String employeeName,int age)
 {
  this.employeeName=employeeName;
  this.age=age;
 
 }
 
 public String getEmployeeName() {
  return employeeName;
 }
 
 public void setEmployeeName(String employeeName) {
  this.employeeName= employeeName;
 }
 
 public int getAge() {
  return age;
 }
 
 public void setAge(int age) {
  this.age = age;
 }
 
 public String toString()
 {
  return getEmployeeName()+" : "+getAge();
 }
}

以经典方式按名称排序员工列表:

我们将以经典的方式对员工列表进行排序

Collections.sort(employeeList,new Comparator() {
   public int compare(Employee e1,Employee e2)
   {
    return e1.getEmployeeName().compareTo(e2.getEmployeeName());
   }
   
  });

创造 EmployeeMain.java

package org.igi.theitroad;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
 
public class EmployeeMain {
 
 public static void main(String[] args)
 {
  List employeeList=getEmployees();
  System.out.println("Before Sorting: ");
  System.out.println(employeeList);
                //in classic way
  Collections.sort(employeeList,new Comparator() {
   public int compare(Employee e1,Employee e2)
   {
    return e1.getEmployeeName().compareTo(e2.getEmployeeName());
   }
   
  });
                System.out.println("After Sorting: ");
                System.out.println(employeeList);
 }
 
 public static List getEmployees()
 {
  List employeesList=new ArrayList();
  Employee e1=new Employee("John", 35);
  Employee e2=new Employee("Adam", 22);
  Employee e3=new Employee("igi", 28);
  Employee e4=new Employee("John", 30);
  Employee e5=new Employee("Grace", 38);
  Employee e6=new Employee("igi", 25);
  employeesList.add(e1);
  employeesList.add(e2);
  employeesList.add(e3);
  employeesList.add(e4);
  employeesList.add(e5);
  employeesList.add(e6);
  return employeesList;
 
 }
}

使用lambda表达式按名称排序员工列表:

我们将按照Lambda表达式对员工列表进行排序,将复杂的比较器语法减少到简单的代码创建 EmployeeMain.java

package org.igi.theitroad;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
 
public class EmployeeMain {
 
 public static void main(String[] args)
 {
     List employeeList=getEmployees();
     System.out.println("Before Sorting: ");
     System.out.println(employeeList);
     //using lambda expression
     Collections.sort(employeeList,(Employee e1,Employee e2)->e1.getEmployeeName().compareTo(e2.getEmployeeName())); 
     System.out.println("After Sorting: "); 
     System.out.println(employeeList);
 }
 
 public static List getEmployees()
 {
     List employeesList=new ArrayList();
     Employee e1=new Employee("John", 35);
     Employee e2=new Employee("Adam", 22);
     Employee e3=new Employee("igi", 28);
     Employee e4=new Employee("John", 30);
     Employee e5=new Employee("Grace", 38);
     Employee e6=new Employee("igi", 25);
     employeesList.add(e1);
     employeesList.add(e2);
     employeesList.add(e3);
     employeesList.add(e4);
     employeesList.add(e5);
     employeesList.add(e6);
     return employeesList;
 
  }
}

运行上述程序,我们将获取以下输出:

Before Sorting:
[John : 35, Adam : 22, igi : 28, John : 30, Grace : 38, igi : 25]
After Sorting:
[Adam : 22, igi : 28, igi : 25, Grace : 38, John : 35, John : 30]]

使用没有类型定义的Lambda表达式对员工列表进行排序

其中我们不会将类型定义传递给lambda表达式中的参数。

它将被解释在上下文中

值需要修改16行中 Employee 名称:

Collections.sort(employeeList,(Employee e1,Employee e2)->e1.getEmployeeName().compareTo(e2.getEmployeeName()));

改成

Collections.sort(employeeList,(e1,e2)->e1.getEmployeeName().compareTo(e2.getEmployeeName()));

反向排序:

要反转列表, 只要修改参数的顺序

将员工主要方法更改为以下方法:

public static void main(String[] args)
 {
     List employeeList=getEmployees();
     System.out.println("Before Sorting: ");
     System.out.println(employeeList);
     //using lambda expression
     Collections.sort(employeeList,(e1,e2)->e2.getEmployeeName().compareTo(e1.getEmployeeName())); 
     System.out.println("After Sorting: ");  
     System.out.println(employeeList);
 }