Lambda 简介:
Lambda 是一个匿名函数,我们可以把 Lambda 表达式理解为是一段可以传递的代码(将代码像数据一样进行传递)。可以写出更简洁、更灵活的代码。作为一种更紧凑的代码风格,使Java的语言表达能力得到了提升。
public class TestLDemo {
@Test
public void test1(){
之前的匿名内部类示例1
Comparator<String> com = new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
};
TreeSet<String> ts = new TreeSet<>(com);
//之前的匿名内部类示例2
List<Integer> list = Arrays.asList(5, 9, 6, 2, 1, 8);
for (Integer num:list) {
System.out.println(num);//5 9 6 2 1 8
}
System.out.println("-------------------------------------");
Collections.sort(list, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o1.compareTo(o2);
}
});
for (Integer num : list) {
System.out.println(num);//1 2 5 6 8 9
}
}
@Test
public void test2(){
//Lambda表达式示例1
Comparator<String> com = (x,y)-> x.compareTo(y);
TreeSet<String> set = new TreeSet<>(com);
//或者
TreeSet<String> set1 = new TreeSet<>((x, y) -> x.compareTo(y));
//Lambda表达式示例2
List<Integer> list = Arrays.asList(5,9,6,2,1,8);
for (Integer num : list) {
System.out.println(num);//5 9 6 2 1 8
}
System.out.println("*******************************");
Collections.sort(list,(x,y)-> x.compareTo(y));
for (Integer num : list) {
System.out.println(num);//1 2 5 6 8 9
}
}
}
下面通过策略设计模式优化代码举例子说明Lambda和Stream API(示例来自于尚硅谷课程)
1、定义实体类
需求
a、获取公司中年龄小于 35 的员工信息
b、获取公司中工资大于 5000 的员工信息
public class Employee {
private int id;
private String name;
private int age;
private double salary;
public Employee() {
}
public Employee(String name) {
this.name = name;
}
public Employee(String name, int age) {
this.name = name;
this.age = age;
}
public Employee(int id, String name, int age, double salary) {
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
public String show() {
return "测试方法引用!";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
long temp;
temp = Double.doubleToLongBits(salary);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
if (age != other.age)
return false;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (Double.doubleToLongBits(salary) != Double.doubleToLongBits(other.salary))
return false;
return true;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", age=" + age + ", salary=" + salary + "]";
}
}
public class TestLDemo {
List<Employee> emps = Arrays.asList(
new Employee(101, "张三", 18, 9999.99),
new Employee(102, "李四", 59, 6666.66),
new Employee(103, "王五", 28, 3333.33),
new Employee(104, "赵六", 8, 7777.77),
new Employee(105, "田七", 38, 5555.55)
);
//需求:获取公司中年龄小于 35 的员工信息
public List<Employee> filterEmployeeAge(List<Employee> emps) {
List<Employee> list = new ArrayList<>();
for (Employee emp : emps) {
if (emp.getAge() <= 35) {
list.add(emp);
}
}
return list;
}
//需求:获取公司中工资大于 5000 的员工信息
public List<Employee> filterEmployeeSalary(List<Employee> emps) {
List<Employee> list = new ArrayList<>();
for (Employee emp : emps) {
if (emp.getSalary() >= 5000) {
list.add(emp);
}
}
return list;
}
@Test
public void test3() {
List<Employee> list = filterEmployeeAge(emps);
for (Employee employee : list) {
System.out.println(employee);
}
System.out.println("------------------------");
List<Employee> list1 = filterEmployeeSalary(emps);
for (Employee employee : list1) {
System.out.println(employee);
}
}
}
2.优化方式一:策略设计模式
不难看出上面完成了需求但是我们发现根据条件过滤员工的时候没增加一个条件都需要增加一个方法
a.首先定义一个策略接口
public interface MyFilter<T> {
public boolean test(T t);
}
b.定义两个实现类去实现接口的策略
//策略1:获取公司中年龄小于 35 的员工信息
public class FilterEmployeeForAge implements MyFilter<Employee>{
@Override
public boolean test(Employee t) {
return t.getAge() <= 35;
}
}
//策略2:获取公司中工资大于 5000 的员工信息
public class FilterEmployeeForSalary implements MyFilter<Employee> {
@Override
public boolean test(Employee t) {
return t.getSalary() >= 5000;
}
}
public class TestLDemo {
List<Employee> emps = Arrays.asList(
new Employee(101, "张三", 18, 9999.99),
new Employee(102, "李四", 59, 6666.66),
new Employee(103, "王五", 28, 3333.33),
new Employee(104, "赵六", 8, 7777.77),
new Employee(105, "田七", 38, 5555.55)
);
//优化方式一:策略设计模式
public List<Employee> filterEmployee(List<Employee> emps, MyFilter<Employee> mp) {
List<Employee> list = new ArrayList<>();
for (Employee employee : emps) {
if (mp.test(employee)) {
list.add(employee);
}
}
return list;
}
@Test
public void test() {
//传入策略1
List<Employee> list = filterEmployee(emps,new FilterEmployeeForAge());
for (Employee employee : list) {
System.out.println(employee);
}
System.out.println("------------------------------------------");
//传入策略2
List<Employee> list2 = filterEmployee(emps, new FilterEmployeeForSalary());
for (Employee employee : list2) {
System.out.println(employee);
}
}
}
3.优化方式二:匿名内部类
问题又来了,我没当定制一个策略的同时,我需要去写一个策略实现类去实现这个策略,那我们可以使用匿名内部类
public class TestLDemo {
List<Employee> emps = Arrays.asList(
new Employee(101, "张三", 18, 9999.99),
new Employee(102, "李四", 59, 6666.66),
new Employee(103, "王五", 28, 3333.33),
new Employee(104, "赵六", 8, 7777.77),
new Employee(105, "田七", 38, 5555.55)
);
public List<Employee> filterEmployee(List<Employee> emps, MyFilter<Employee> mp) {
List<Employee> list = new ArrayList<>();
for (Employee employee : emps) {
if (mp.test(employee)) {
list.add(employee);
}
}
return list;
}
@Test
public void test() {
//匿名内部类实现策略1
List<Employee> list = filterEmployee(emps, new MyFilter<Employee>() {
@Override
public boolean test(Employee employee) {
return employee.getAge() <= 35;
}
});
for (Employee employee : list) {
System.out.println(employee);
}
System.out.println("------------------------------------------");
//匿名内部类实现策略2
List<Employee> list2 = filterEmployee(emps, new MyFilter<Employee>() {
@Override
public boolean test(Employee employee) {
return employee.getSalary() >= 5000;
}
});
for (Employee employee : list2) {
System.out.println(employee);
}
}
}
4.优化方式三:Lambda表达式
public class TestLDemo {
List<Employee> emps = Arrays.asList(
new Employee(101, "张三", 18, 9999.99),
new Employee(102, "李四", 59, 6666.66),
new Employee(103, "王五", 28, 3333.33),
new Employee(104, "赵六", 8, 7777.77),
new Employee(105, "田七", 38, 5555.55)
);
public List<Employee> filterEmployee(List<Employee> emps, MyFilter<Employee> mp) {
List<Employee> list = new ArrayList<>();
for (Employee employee : emps) {
if (mp.test(employee)) {
list.add(employee);
}
}
return list;
}
@Test
public void test() {
//Lambda匿名内部类实现策略1
List<Employee> list = filterEmployee(emps, (e)-> e.getAge() <= 35);
list.forEach(System.out::println);//Stream API
System.out.println("------------------------------------------");
//Lambda匿名内部类实现策略2
List<Employee> list2 = filterEmployee(emps, (e) -> e.getSalary() >= 5000);
list2.forEach(System.out::println);//Stream API
}
}
5.优化方式4:Stream API简化代码
public class TestLDemo {
List<Employee> emps = Arrays.asList(
new Employee(101, "张三", 18, 9999.99),
new Employee(102, "李四", 59, 6666.66),
new Employee(103, "王五", 28, 3333.33),
new Employee(104, "赵六", 8, 7777.77),
new Employee(105, "田七", 38, 5555.55)
);
public List<Employee> filterEmployee(List<Employee> emps, MyFilter<Employee> mp) {
List<Employee> list = new ArrayList<>();
for (Employee employee : emps) {
if (mp.test(employee)) {
list.add(employee);
}
}
return list;
}
@Test
public void test() {
emps.stream()
.filter((e) -> e.getAge() <= 35)
.forEach(System.out::println);
System.out.println("----------------------------------------------");
emps.stream()
.filter((e) -> e.getSalary() >= 5000)
.forEach(System.out::println);
System.out.println("----------------------------------------------");
emps.stream()
.map(Employee::getName)
.limit(3)
.sorted()
.forEach(System.out::println);
}
}