java8新特性Stream和lambda案例(策略模式)
1、Employee类
package com.example.demo; public class Employee { private String name; private int age; private double salary; @Override public String toString() { return "Employee{" + "name='" + name + '\'' + ", age=" + age + ", salary=" + salary + '}'; } 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; } }
2、IEmployee接口
package com.example.demo; public interface IEmployee<T> { public boolean test(T t); }
3、FilterEmployeeBySalary实现类
package com.example.demo; public class FilterEmployeeBySalary implements IEmployee<Employee>{ @Override public boolean test(Employee t) { return t.getSalary()>2; } }
4、FilterEmployeeByAge实现类
package com.example.demo; public class FilterEmployeeByAge implements IEmployee<Employee>{ @Override public boolean test(Employee t) { return t.getAge() > 18; } }
5、DemoApplicationTests测试方法
package com.example.demo; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @SpringBootTest class DemoApplicationTests { List<Employee> list = Arrays.asList( new Employee("A", 18, 2), new Employee("B", 20, 3), new Employee("C", 30, 4), new Employee("D", 11, 5) ); @Test void contextLoads() { List<Employee> employees = filterEmployees(list); for (Employee emp : employees) { System.out.println(emp.toString()); } } //需求1:获取年龄大于18的员工 public List<Employee> filterEmployees(List<Employee> list) { List<Employee> emps = new ArrayList<>(); for (Employee emp : list) { if (emp.getAge() > 18) { emps.add(emp); } } return emps; } //需求2:获取工资大于2的员工 public List<Employee> filterEmployees2(List<Employee> list) { List<Employee> emps = new ArrayList<>(); for (Employee emp : list) { if (emp.getSalary() > 2) { emps.add(emp); } } return emps; } @Test void test1() { List<Employee> employees = filterEmployee(list, new FilterEmployeeByAge()); for (Employee emp : employees) { System.out.println(emp.toString()); } List<Employee> employees2 = filterEmployee(list, new FilterEmployeeBySalary()); for (Employee emp : employees) { System.out.println(emp.toString()); } } //优化1:策略设计模式优化 public List<Employee> filterEmployee(List<Employee> list, IEmployee<Employee> mp) { List<Employee> emps = new ArrayList<>(); for (Employee emp : list) { if (mp.test(emp)) { emps.add(emp); } } return emps; } //优化2:匿名内部类优化(相比于1不用创建更多的类) @Test void test2() { final List<Employee> employees = filterEmployee(list, new IEmployee<Employee>() { @Override public boolean test(Employee employee) { return employee.getAge() > 18; } }); for (Employee emp : employees) { System.out.println(emp.toString()); } } //优化3:Lambada表达式(相对于匿名内部类优化) @Test void test3() { final List<Employee> employees = filterEmployee(list, (e) -> e.getAge() > 18); employees.forEach(System.out::println); } //优化4:Stream API @Test void test4() { list.stream() .filter((e) -> e.getAge() > 18) .forEach(System.out::println); //获取所有的名字 list.stream().map(Employee::getName).forEach(System.out::println); } }