Lambda是一个匿名函数,可以将Lambda表达式理解为是一段可以传递代码。
package com.expgiga.Java8;
/**
*
*/
public class Employee {
private String name;
private int age;
private double salary;
public Employee() {
}
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 +
'}';
}
}
package com.expgiga.Java8;
/**
*
*/
public interface MyPredicate<T> {
public boolean test(T t);
}
package com.expgiga.Java8;
/**
*
*/
public class FilterEmployeeByAge implements MyPredicate<Employee> {
@Override
public boolean test(Employee employee) {
return employee.getAge() > 16;
}
}
package com.expgiga.Java8;
/**
*
*/
public class FilterEmployeeBySalary implements MyPredicate<Employee> {
@Override
public boolean test(Employee employee) {
return employee.getSalary() > 19999;
}
}
package com.expgiga.Java8;
import java.util.*;
/**
*
*/
public class TestLambda {
public static void main(String[] args) {
//原来的匿名内部类
Comparator<Integer> com = new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return Integer.compare(o1, o2);
}
};
TreeSet<Integer> ts = new TreeSet<>(com);
//lambda表达式
Comparator<Integer> comparator = (x, y) -> Integer.compare(x, y);
TreeSet<Integer> treeSet = new TreeSet<>(comparator);
//需求:获取当前公司中员工年龄大于16的员工信息
List<Employee> employeeList = Arrays.asList(
new Employee("zhangsan", 18, 19999),
new Employee("lisi", 28, 29999),
new Employee("wangwu", 38, 39999),
new Employee("zhaoliu", 16, 17999),
new Employee("tianqi", 6, 12999)
);
List<Employee> list = filterEmployees(employeeList);
for (Employee e : list) {
System.out.println(e);
}
//需求,获取当前公司中员工工资大于19999的员工的信息
List<Employee> list2 = filterEmployees2(employeeList);
for (Employee e : list2) {
System.out.println(e);
}
//优化代码一:策略设计模式
List<Employee> list3 = filterEmployee(employeeList, new FilterEmployeeByAge());
for (Employee e : list3) {
System.out.println(e);
}
List<Employee> list4 = filterEmployee(employeeList, new FilterEmployeeBySalary());
for (Employee e : list4) {
System.out.println(e);
}
//优化代码二:匿名内部类
List<Employee> list5 = filterEmployee(employeeList, new MyPredicate<Employee>() {
@Override
public boolean test(Employee employee) {
return employee.getSalary() < 19999;
}
});
for (Employee employee : list5) {
System.out.println(employee);
}
//优化代码三:Lambda表达式
List<Employee> list6 = filterEmployee(employeeList, (e) -> e.getSalary() < 19999);
for (Employee e : list6) {
System.out.println(e);
}
System.out.println("------------------------------");
//优化代码四:Stream API
employeeList.stream().filter((e) -> e.getSalary() < 19999).limit(1).forEach(System.out::println);
System.out.println("==============================");
employeeList.stream().map(Employee::getName).forEach(System.out::println);
}
public static List<Employee> filterEmployees(List<Employee> list) {
List<Employee> emps = new ArrayList<>();
for (Employee e : list) {
if (e.getAge() > 16) {
emps.add(e);
}
}
return emps;
}
public static List<Employee> filterEmployees2(List<Employee> list) {
List<Employee> emps = new ArrayList<>();
for (Employee e : list) {
if (e.getSalary() > 19999) {
emps.add(e);
}
}
return emps;
}
//优化代码一:策略设计模式
public static List<Employee> filterEmployee(List<Employee> list, MyPredicate<Employee> employeeMyPredicate) {
List<Employee> emps = new ArrayList<>();
for (Employee employee : list) {
if (employeeMyPredicate.test(employee)) {
emps.add(employee);
}
}
return emps;
}
//优化代码二:匿名内部类
}