# 【前言】

Lamda是一个匿名函数，可以把lamda表达式理解为一段可以传递的代码，可以写出简洁，更加灵活的代码。作为一种更加紧凑的代码风格，使Java的语言表达式能力得到了提升。

# 【内容】

## 1.为什么要用Lamda表达式？

//员工的集合：
List<Employee> employeeList= Arrays.asList(
new Employee("张三",12,123.12),
new Employee("李四",38,123.123),
new Employee("王五",50,123.123),
new Employee("赵六",16,123.123),
new Employee("田七",8,123.123)
);

//员工实体
/**
* Created by cyl on 2018/6/4.
*/
public class Employee {
private String name;
private Integer age;
private double salary;
public String getName() {
return name;
}

public double getSalary() {
return salary;
}

public void setSalary(double salary) {
this.salary = salary;
}

public void setName(String name) {
this.name = name;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

/**
* 获取员工的基本信息
* @param name
* @param age
* @param salary
*/
public  Employee(String name,Integer age,double salary){
this.name=name;
this.age=age;
this.salary=salary;
}

@Override
public String toString() {
return super.toString()+"["+this.name+","+this.age+","+this.salary+"]";
}
}


### 通过普通的方式

//需求：获取当前公司中员工年龄大于35的年龄
public List<Employee> filterEmployeesByAge(List<Employee> list){
List<Employee> emps=new ArrayList<>();
for (Employee emp : list) {
if(emp.getAge()>=35) {
}
}
return emps;
}

//需求：获取当前公司中员工工资小于5000的人
public List<Employee> filterEmployeesBySalary(List<Employee> list){
List<Employee> emps=new ArrayList<>();
for (Employee emp : list) {
if(emp.getSalary()>=35) {
}
}
return emps;
}

@Test
public  void test3(){
List<Employee> listAge=filterEmployeesByAge(employeeList);
List<Employee> listSalary=filterEmployeesBySalary(employeeList);

}

### 抽出比较的条件，使用策略模式的方法

1.创建比较接口

package com.cyl.Lamda;

/**
* Created by cyl on 2018/6/4.
*/
public interface MyPredicate<T> {
boolean test(T t);
}

package com.cyl.Lamda;

/**
* Created by cyl on 2018/6/4.
*/
public class FilterEmployeeByAge implements MyPredicate<Employee> {
@Override
public boolean test(Employee employee) {
return employee.getAge()>35;
}
}
package com.cyl.Lamda;

/**
* Created by cyl on 2018/6/4.
*/
public class FilterEmployeeBySalaryimplements MyPredicate<Employee> {
@Override
public boolean test(Employee employee) {
return employee.getSalary()>35;
}
}

3.重构filterEmployee方法

 public  List<Employee>  filterEmployee(List<Employee> list,MyPredicate<Employee> mp){
List<Employee> employees=new ArrayList<>();
for (Employee employee : list) {
if(mp.test(employee)){
}
}
return  employees;
}

4.测试：

/**
* 优化方式一：通过策略设计模式
*/
public void test4(){
filterEmployee(employeeList,new FilterEmployeeByAge());
filterEmployee(employeeList,new FilterEmployeeBySalary());
}

### 匿名内部类直接测试

//优化方式二：匿名内部类
@Test
public  void test5(){
//匿名内部类
List<Employee> list= filterEmployee(employeeList, new MyPredicate<Employee>() {
@Override
public boolean test(Employee employee) {
return employee.getSalary()<=5000;
}
});
for (Employee employee : list) {
System.out.println(employee);
}
}

### Lamda表达式直接测试：

    @Test
public  void test6(){
List<Employee> list=filterEmployee(employeeList,(e)->e.getSalary()<=5000);
list.forEach(System.out::println);
}

## 3.Lamda表达式结构

   左侧是参数列表，右侧是方法的具体实现；一个参数省去括号，多条函数体用{}