Lambda表达式
Lambda表达式可以看成是匿名内部类,Lambda 允许把函数作为一个方法的参数(函数作为方法参数传递),将代码像数据一样传递,使用 Lambda 表达式可以使代码变的更加简洁紧凑。
Lambda表达需要函数式接口的支持。
基本语法:
<函数式接口> <变量名> = (参数1,参数2...) -> {
//方法体
};
案例1:
//匿名内部类
Runnable r=new Runnable() {
@Override
public void run() {
System.out.println("hello");
}
};
//lambda表达式
Runnable r2=()->System.out.println("hello");
案例2:
//匿名内部类
Comparator<String> comparator=new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
// TODO Auto-generated method stub
return o1.length()-o2.length();
}
};
TreeSet<String> treeSet=new TreeSet<String>(comparator);
//lambda表达式
TreeSet<String> treeSet2=new TreeSet<>((o1,o2)->o1.length()-o2.length());
为什么使用lambda表达式
需求1:有一个员工集合,获取年龄大于25的员工信息
package com.qf.day15_3;
public class Employee {
private String name;
private int age;
private double salary;
public Employee() {
// TODO Auto-generated constructor stub
}
public Employee(String name, int age, double salary) {
this.name = name;
this.age = age;
this.salary=salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee [name=" + name + ", age=" + age + ", salary=" + salary + "]";
}
}
public static void main(String[] args) {
List<Employee> employees=new ArrayList<>();
employees.add(new Employee("xxx", 30, 10000));
employees.add(new Employee("yyy", 29, 8000));
employees.add(new Employee("zzz", 22, 12000));
employees.add(new Employee("张三", 21, 20000));
employees.add(new Employee("李四", 32, 22000));
List<Employee> list = filterEmployee(employees);
for (Employee emp : list) {
System.out.println(emp.toString());
}
}
public static List<Employee> filterEmployee(List<Employee> employees) {
List<Employee> list=new ArrayList<>();
for (Employee e : employees) {
if(e.getAge()>=25) {
list.add(e);
}
}
return list;
}
需求2:获取工资大于10000的员工信息
public static List<Employee> filterEmployee2(List<Employee> employees) {
List<Employee> list=new ArrayList<>();
for (Employee e : employees) {
if(e.getSalary()>=10000) {
list.add(e);
}
}
return list;
}
问题:如果再添加类似需求,需要再添加一个方法。如何解决?
1.使用策略设计模式
package com.qf.day15_3;
/**
*定义一个接口
*/
public interface MyPredicate<T> {
boolean test(T t);
}
//两个实现类
package com.qf.day15_3;
//按照年龄过滤
class FilterEmployeeByAge implements MyPredicate<Employee>{
@Override
public boolean test(Employee t) {
// TODO Auto-generated method stub
return t.getAge()>=25;
}
}
//按照工资过滤
class FilterEmployeeBySalary implements MyPredicate<Employee>{
@Override
public boolean test(Employee t) {
// TODO Auto-generated method stub
return t.getSalary()>=10000;
}
}
public static void main(String[] args) {
List<Employee> employees=new ArrayList<>();
employees.add(new Employee("xxx", 30, 10000));
employees.add(new Employee("yyy", 29, 8000));
employees.add(new Employee("zzz", 22, 12000));
employees.add(new Employee("张三", 21, 20000));
employees.add(new Employee("李四", 32, 22000));
//按照年龄过滤
System.out.println("-------------按照年龄过滤--------------");
List<Employee> list=filterEmployee(employees,new FilterEmployeeByAge());
for (Employee employee : list) {
System.out.println(employee.toString());
}
System.out.println("-------------按照工资过滤--------------");
//按照工资过滤
List<Employee> list2=filterEmployee(employees,new FilterEmployeeBySalary());
for (Employee employee : list2) {
System.out.println(employee.toString());
}
}
public static List<Employee> filterEmployee(List<Employee> employees,MyPredicate<Employee> predicate) {
List<Employee> list=new ArrayList<>();
for (Employee e : employees) {
if(predicate.test(e)) {
list.add(e);
}
}
return list;
}
2:使用Lambada表达式优化策略模式。
public static void main(String[] args) {
List<Employee> employees=new ArrayList<>();
employees.add(new Employee("xxx", 30, 10000));
employees.add(new Employee("yyy", 29, 8000));
employees.add(new Employee("zzz", 22, 12000));
employees.add(new Employee("张三", 21, 20000));
employees.add(new Employee("李四", 32, 22000));
//按照年龄过滤
System.out.println("-------------按照年龄过滤--------------");
List<Employee> list=filterEmployee(employees,(e)->e.getAge()>=25);
for (Employee employee : list) {
System.out.println(employee.toString());
}
System.out.println("-------------按照工资过滤--------------");
//按照工资过滤
List<Employee> list2=filterEmployee(employees,(e)->e.getSalary()>=10000);
for (Employee employee : list2) {
System.out.println(employee.toString());
}
}
public static List<Employee> filterEmployee(List<Employee> employees,MyPredicate<Employee> predicate) {
List<Employee> list=new ArrayList<>();
for (Employee e : employees) {
if(predicate.test(e)) {
list.add(e);
}
}
return list;
}
3:使用Stream API再优化lambda表达式
public static void main(String[] args) {
List<Employee> employees = new ArrayList<>();
employees.add(new Employee("xxx", 30, 10000));
employees.add(new Employee("yyy", 29, 8000));
employees.add(new Employee("zzz", 22, 12000));
employees.add(new Employee("张三", 21, 20000));
employees.add(new Employee("李四", 32, 22000));
// 按照年龄过滤
System.out.println("-------------按照年龄过滤--------------");
employees
.stream()
.filter((e) -> e.getAge() >= 25)
.forEach(System.out::println);
// 按照工资过滤
System.out.println("-------------按照工资过滤--------------");
employees
.stream()
.filter((e) -> e.getSalary() >= 10000)
.forEach(System.out::println);
}
使用lambda表达式注意事项
Lambda引入了新的操作符:->(箭头操作符),->将表达式分成两部分
左侧:(参数1,参数2…)表示参数列表;
右侧:{}内部是方法体
1、形参列表的数据类型会自动推断;
2、如果形参列表为空,只需保留();
3、如果形参只有1个,()可以省略,只需要参数的名称即可;
4、如果执行语句只有1句,且无返回值,{}可以省略,若有返回值,则若想省去{},则必须同时省略return,且执行语句也保证只有1句;
5、lambda不会生成一个单独的内部类文件;
6、lambda表达式若访问了局部变量,则局部变量必须是final的,若是局部变量没有加final关键字,系统会自动添加,此后在修改该局部变量,会报错。