java8—lambda表达式(一)

  • Post author:
  • Post category:java



<1.初识lambda表达式>


需求1:获取当前公司中当前员工年龄大于35的员工信息

/**
 * 定义一个员工角色类
 */
@Data
@ToString
public class Employee{
   private String name;
   private int age;
   private double salary;
   public Employee(){}
   public Employee(String name,int age,double salary){
   this.name = name;
   this.age = age;
   this.salary = salary; 
}
} 
//引入junit Test测试工具类
import org.junit.Test
public class Test{
   //初始化员工信息
   List<Employee> emps = Arrays.asList(new Employee("Kiven",24,10000),new Employee("Nick",40,39999),new Employee("Lily",25,8000));
   @Test
   public void test1(){
   List<Employee> newEmps = filterEmployees(emps);
   for(Employee employee:newEmps){
   System.out.println(employee);
}
}
   private List<Employee> filterEmployees(List<Employee> emps){
   List<Employee> list = new ArrayList<>();
   for(Employee emp : list){
      if(emp.getAge()>35){
       list.add(emp);
}
    return list ;
}
}


需求2:获取当前公司中员工薪水大于5000的员工

//引入junit Test测试工具类
import org.junit.Test
public class Test{
   //初始化员工信息
   List<Employee> emps = Arrays.asList(new Employee("Kiven",24,10000),new Employee("Nick",40,39999),new Employee("Lily",25,8000));
   @Test
   public void test1(){
   List<Employee> newEmps = filterEmployees(emps);
   for(Employee employee:newEmps){
   System.out.println(employee);
}
}
   private List<Employee> filterEmployees(List<Employee> emps){
   List<Employee> list = new ArrayList<>();
   for(Employee emp : list){
      if(emp.getSalary()>5000){    //相比需求1的实现只改变了一行代码
       list.add(emp);
}
    return list;
}
}


优化方式1:策略模式,

定义一系列算法,将每个算法单独封装,使之可以相互替换,具体方法策略的选择由客户端决定。

/**
 * 定义一个条件判断接口
 */
public interface MyPredicate<T>{
   boolean test(T t);
}
/**
 * 条件接口的具体实现-根据年龄过滤
 */
public class FilterEmployeeByAge{
  @Override 
  public boolean test(Employee e){
     return e.getAge >35;
}
}
/**
 * 条件接口的具体实现-根据薪水过滤
 */
public class FilterEmployeeBySalary{
  @Override 
  public boolean test(Employee e){
     return e.getSalary>5000;
}
}
//引入junit Test测试工具类
import org.junit.Test
public class Test{
   //初始化员工信息
   List<Employee> emps = Arrays.asList(new Employee("Kiven",24,10000),new Employee("Nick",40,39999),new Employee("Lily",25,8000));
   @Test
   public void test1(){
   List<Employee> newEmps = filterEmployees(emps,new FilterEmployeeByAge());  //适应各种判断条件,而不需要改变filterEmployees()方法,策略模式
   for(Employee employee:newEmps){
   System.out.println(employee);
}
}
   private List<Employee> filterEmployees(List<Employee> emps,MyPredicate<T> mp){ //新增一个MyPredicat类型的参数
   List<Employee> list = new ArrayList<>();
   for(Employee emp : list){
      if(mp.test(emp)){
       list.add(emp);
}
    return list;
}
}


优化方式2:匿名内部类,需要MyPredicate接口的具体实现,通过匿名内部类实现具体的操作

/**
 * 定义一个条件判断接口
 */
public interface MyPredicate<T>{
   boolean test(T t);
}定义一个条件判断接口
 */
public interface MyPredicate<T>{
   boolean test(T t);
}
//引入junit Test测试工具类
import org.junit.Test
public class Test{
   //初始化员工信息
   List<Employee> emps = Arrays.asList(new Employee("Kiven",24,10000),new Employee("Nick",40,39999),new Employee("Lily",25,8000));
   @Test
   public void test1(){
   List<Employee> list = filterEmployee(employees, new MyPredicate<Employee>() {  //匿名内部类
     @Override
     public boolean test(Employee employee) {
          return employee.getSalary() <= 5000;
     }
    });
    for (Employee employee : list) {
        System.out.println(employee);
     }
}
   private List<Employee> filterEmployees(List<Employee> emps,MyPredicate<T> mp){ //新增一个MyPredicat类型的参数
   List<Employee> list = new ArrayList<>();
   for(Employee emp : list){
      if(mp.test(emp)){
       list.add(emp);
}
    return list;
}
}


优化方式3:lambda表达式

/**
 * 定义一个条件判断接口
 */
public interface MyPredicate<T>{
   boolean test(T t);
}
//引入junit Test测试工具类
import org.junit.Test
public class Test{
   //初始化员工信息
   List<Employee> emps = Arrays.asList(new Employee("Kiven",24,10000),new Employee("Nick",40,39999),new Employee("Lily",25,8000));
   @Test
   public void test1(){
   List<Employee> list = filterEmployee(employees, employee -> employee.getSalary() >= 5000); //lambda表达式
   list.forEach(System.out::println);
}
}
   private List<Employee> filterEmployees(List<Employee> emps,MyPredicate<T> mp){ //新增一个MyPredicat类型的参数
   List<Employee> list = new ArrayList<>();
   for(Employee emp : list){
      if(mp.test(emp)){
       list.add(emp);
}
    return list;
}
}


优化方式4:streamApi

//引入junit Test测试工具类
import org.junit.Test
public class Test{
   public void test(){
   //streamApi 一步到位~
   emps.stream().filter(e->e.getSalary()>=5000).forEach(System.out::println);
}
}
}


<2.lambda表达式基础语法>:Java8中引入一个新的操作符 “->”,该操作符称为箭头操作符或lambda操作符


箭头操作符将lambda表达式拆分成两部分:


左侧:lambda表达式的参数列表


右侧:lambda表达式所需要执行的功能,即lambda体

①语法格式1:无参数,无返回值

()->System.out.println("hello lambda!");

②语法格式2:有一个参数,无返回值

Consumer c = x -> System.out.println(x);
c.accept("hello");

③语法格式3:有两个以上的的参数,并且lambda体中有多条语句

Comparator<Integer> com = (x,y) -> {
   System.out.println("函数式接口");
   return Integer.compare(x,y);
};
System.out.println(com.compare(2,2));


备注:


*左侧若只有一个参数,小括号可以不写


*若lambda体中只有一条语句,return 和 大括号都可以不写


*lambda表达式参数列表的参数类型省略不写,JVM编译器可以通过上下文推断出类型,即“类型推断”



版权声明:本文为zy52002520原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。