一、为什么使用Lambda表达式
Lambda是一个匿名函数,我们可以把Lambda表达式理解为是一段可以传递的代码(将代码像数据一样进行传递)。可以写出更简洁、更灵活的代码。作为一种更紧凑的代码风格,使java的语言表达能力得到了提升。
二、匿名内部类的方式与Lambda表达式方式的比较(示例)
场景:比较2个数,一种采用内部类的方式,一种采用lambda表达式的方式;如下图:
三、需求演示示例
场景演示:获取员工年龄小于35的员工信息,然后再获取员工工资大于5000的员工信息。
一、创建一个员工实体类
package com.xz.springboot_java8.day1.entity;
public class Employee {
private int id;//id
private String name;//名称
private int age;//年龄
private Double salary; //薪水
//无参构造方法
public Employee() { }
//带参构造方法
public Employee(int id, String name, int age, Double salary) {
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
}
//getter、setter、及toString方法此处省略
........
}
二、普通方式实现
1、代码如下
package com.xz.springboot_java8.day1.test;
import com.xz.springboot_java8.day1.entity.Employee;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Test1 {
public static void main(String[] args) {
//初始化员工数据并转成list
List<Employee> emps = Arrays.asList(
new Employee(1, "张三", 18, 2222.22),
new Employee(2, "李四", 25, 3333.33),
new Employee(3, "王五", 38, 5555.55),
new Employee(4, "赵六", 45, 8888.88)
);
//调用年龄小于35的员工信息方法
List<Employee> list = filterEmployeeAge(emps);
for (Employee employee : list) {
System.out.println(employee);
}
System.out.println("============================");
//调用工资大于5000的员工信息方法
List<Employee> list1 = filterEmployeeSalary(emps);
for (Employee employee : list1) {
System.out.println(employee);
}
}
public static List<Employee> filterEmployeeAge(List<Employee> emps){
List<Employee> list = new ArrayList<>();
for (Employee emp : emps) {
if(emp.getAge() <= 35){
list.add(emp);
}
}
return list;
}
public static List<Employee> filterEmployeeSalary(List<Employee> emps){
List<Employee> list = new ArrayList<>();
for (Employee emp : emps) {
if(emp.getSalary() >= 5000){
list.add(emp);
}
}
return list;
}
}
2、运行main函数,输出结果如下图:
3、 普通方式实现的缺点
每新增一个新的需求就要重新写一个方法,方法中只有判断逻辑部分不同,其他都相同,出现代码冗余。如下图:
三、优化方式一(策略模式实现)
1、定义一个策略接口
package com.xz.springboot_java8.day1.test2;
public interface MyStrategy<T> {
//该方法默认为public abstract修饰
public abstract boolean getEmployeeInfo(T t);
}
2、定义一个过滤年龄的接口实现类
package com.xz.springboot_java8.day1.test2;
import com.xz.springboot_java8.day1.entity.Employee;
public class FilterEmployeeAge implements MyStrategy<Employee>{
@Override
public boolean getEmployeeInfo(Employee employee) {
return employee.getAge() <= 35;
}
}
3、定义一个过滤薪资的接口实现类
package com.xz.springboot_java8.day1.test2;
import com.xz.springboot_java8.day1.entity.Employee;
public class FilterEmployeeSalary implements MyStrategy<Employee>{
@Override
public boolean getEmployeeInfo(Employee employee) {
return employee.getSalary() >= 5000;
}
}
4、测试类如下:
package com.xz.springboot_java8.day1.test2;
import com.xz.springboot_java8.day1.entity.Employee;
import java.util.*;
public class test {
public static void main(String[] args) {
//初始化员工数据并转成list
List<Employee> emps = Arrays.asList(
new Employee(1, "张三", 18, 2222.22),
new Employee(2, "李四", 25, 3333.33),
new Employee(3, "王五", 38, 5555.55),
new Employee(4, "赵六", 45, 8888.88)
);
//调用年龄小于35的员工信息方法
List<Employee> list = getEmployee(emps,new FilterEmployeeAge());
for (Employee employee : list) {
System.out.println(employee);
}
System.out.println("------------------------");
//调用调用工资大于5000的员工信息方法
List<Employee> list1 = getEmployee(emps,new FilterEmployeeSalary());
for (Employee employee : list1) {
System.out.println(employee);
}
}
public static List<Employee> getEmployee(List<Employee> list,MyStrategy<Employee> strategy){
List<Employee> empList = new ArrayList<>();
for(Employee emp:list){
if(strategy.getEmployeeInfo(emp)){
empList.add(emp);
}
}
return empList;
}
}
5、运行main函数,输出如下:
6、 策略模式实现的缺点
每新增一个新的需求就要重新写一个实现策略接口的实现类,类图如下:
四、优化方式二(匿名内部类实现)
1、定义一个策略接口
package com.xz.springboot_java8.day1.test2;
public interface MyStrategy<T> {
//该方法默认为public abstract修饰
public abstract boolean getEmployeeInfo(T t);
}
2、测试类如下:
package com.xz.springboot_java8.day1.test3;
import com.xz.springboot_java8.day1.entity.Employee;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Test {
public static void main(String[] args) {
//初始化员工数据并转成list
List<Employee> emps = Arrays.asList(
new Employee(1, "张三", 18, 2222.22),
new Employee(2, "李四", 25, 3333.33),
new Employee(3, "王五", 38, 5555.55),
new Employee(4, "赵六", 45, 8888.88)
);
//调用年龄小于35的员工信息方法(匿名内部类的方式)
List<Employee> list = getEmployee(emps, new MyStrategy<Employee>(){
@Override
public boolean getEmployeeInfo(Employee employee) {
return employee.getAge() <= 35;
}
});
for (Employee employee : list) {
System.out.println(employee);
}
System.out.println("------------------------");
//调用调用工资大于5000的员工信息方法(匿名内部类的方式)
List<Employee> list1 = getEmployee(emps, new MyStrategy<Employee>(){
@Override
public boolean getEmployeeInfo(Employee employee) {
return employee.getSalary() >= 5000;
}
});
for (Employee employee : list1) {
System.out.println(employee);
}
}
public static List<Employee> getEmployee(List<Employee> list, MyStrategy<Employee> strategy){
List<Employee> empList = new ArrayList<>();
for(Employee emp:list){
if(strategy.getEmployeeInfo(emp)){
empList.add(emp);
}
}
return empList;
}
}
3、运行main函数,输出如下:
五、优化方式三(Lambda实现)
1、定义一个策略接口
package com.xz.springboot_java8.day1.test2;
public interface MyStrategy<T> {
//该方法默认为public abstract修饰
public abstract boolean getEmployeeInfo(T t);
}
2、测试类如下:
package com.xz.springboot_java8.day1.test4;
import com.xz.springboot_java8.day1.entity.Employee;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Test {
public static void main(String[] args) {
//初始化员工数据并转成list
List<Employee> emps = Arrays.asList(
new Employee(1, "张三", 18, 2222.22),
new Employee(2, "李四", 25, 3333.33),
new Employee(3, "王五", 38, 5555.55),
new Employee(4, "赵六", 45, 8888.88)
);
//调用年龄小于35的员工信息方法(Lambda表达式实现)
List<Employee> list = getEmployee(emps, (e) -> e.getAge() <= 35);
for (Employee employee : list) {
System.out.println(employee);
}
System.out.println("------------------------");
//调用调用工资大于5000的员工信息方法(Lambda表达式实现)
List<Employee> list1 = getEmployee(emps, (e) -> e.getSalary() >= 5000);
for (Employee employee : list1) {
System.out.println(employee);
}
}
public static List<Employee> getEmployee(List<Employee> list, MyStrategy<Employee> strategy){
List<Employee> empList = new ArrayList<>();
for(Employee emp:list){
if(strategy.getEmployeeInfo(emp)){
empList.add(emp);
}
}
return empList;
}
}
3、运行main函数,输出如下:
六、优化方式四(Stream API和Lambda表达式实现)
1、代码如下:
package com.xz.springboot_java8.day1.test5;
import com.xz.springboot_java8.day1.entity.Employee;
import java.util.Arrays;
import java.util.List;
public class Test {
public static void main(String[] args) {
//初始化员工数据并转成list
List<Employee> emps = Arrays.asList(
new Employee(1, "张三", 18, 2222.22),
new Employee(2, "李四", 25, 3333.33),
new Employee(3, "王五", 38, 5555.55),
new Employee(4, "赵六", 45, 8888.88)
);
//调用年龄小于35的员工信息方法(Lambda表达式实现)
emps.stream()
.filter((e)->e.getAge() <= 35)
.forEach(System.out::println);
System.out.println("=======================");
//调用调用工资大于5000的员工信息方法(Lambda表达式实现)
emps.stream()
.filter((e)->e.getSalary() >= 5000)
.forEach(System.out::println);
}
}
2、运行main函数,输出结果如下:
四、示例演示总结
由以上优化示例可知,lambda表达式可以传递的代码(将代码像数据一样进行传递)并且可以写出更简洁、更灵活的代码。
总结
本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注编程网的更多内容!