本文主要是介绍Java8新特性-为什么使用Lambda表达式,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
package com.zs.boot.controller;
import org.junit.Test;
import java.util.*;
public class TestLambda {
//原来的匿名内部类
@Test
public void test1(){
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表达式
@Test
public void test2(){
Comparator<Integer> com = (x,y) -> Integer.compare(x,y);
TreeSet<Integer> ts = new TreeSet<>(com);
}
//==================================================================
List<Employee> employees = Arrays.asList(
new Employee("刘德华",48,8000),
new Employee("张学友",28,8500),
new Employee("黎明",45,6500),
new Employee("郭富城",51,5500));
public List<Employee> filterEmployees(List<Employee> list){
List<Employee> empList = new ArrayList<>();
for(Employee emp:employees){
if(emp.getAge() >= 35){
empList.add(emp);
}
}
return empList;
}
//需求:获取当前公司中员工年龄大于35的员工信息
@Test
public void test3(){
List<Employee> employeeList = filterEmployees(employees);
for (Employee employee:employeeList) {
System.out.println(employee);
}
}
//有来了一个需求:获取公司中工资大于 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;
}
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 +
'}';
}
}
//====================================================================
//优化方式一:策略设计模式
public interface MyPredicate<T>{
public boolean test(T t);
}
public class FilterEmployeeByAge implements MyPredicate<Employee>{
@Override
public boolean test(Employee employee) {
return employee.getAge() >=35;
}
}
public class FilterEmployeeBySalary implements MyPredicate<Employee>{
@Override
public boolean test(Employee employee) {
return employee.getSalary() >=8000;
}
}
public List<Employee> filterEmployee(List<Employee> emps, MyPredicate<Employee> mp){
List<Employee> list = new ArrayList<>();
for (Employee employee : emps) {
if(mp.test(employee)){
list.add(employee);
}
}
return list;
}
@Test
public void test4(){
List<Employee> employeeList = filterEmployee(employees,new FilterEmployeeByAge());
for (Employee employee: employeeList) {
System.out.println(employee);
}
System.out.println("--------------111-------------------");
List<Employee> employeeList2 = filterEmployee(employees,new FilterEmployeeBySalary());
for (Employee employee: employeeList2) {
System.out.println(employee);
}
System.out.println("---------------2222------------------");
}
//==================================================================
//优化方式二:匿名内部类
@Test
public void test5(){
List<Employee> employeeList = filterEmployee(employees, new MyPredicate<Employee>() {
@Override
public boolean test(Employee employee) {
return employee.getSalary() >=8000;
}
});
for (Employee employee:employeeList) {
System.out.println(employee);
}
}
//==================================================================
//优化方式三:Lambda 表达式
@Test
public void test6(){
List<Employee> employeeList = filterEmployee(employees,(e) -> e.getSalary() >=8000);
/*for (Employee employee:employeeList) {
System.out.println(employee);
}*/
employeeList.forEach(System.out::println);
}
//==================================================================
//优化方式四:Stream API
@Test
public void test7(){
employees.stream()
.filter((e) -> e.getAge() >=30)
.forEach(System.out::println);
System.out.println("----------------------------------------------");
employees.stream()
.map(Employee::getName)
.limit(3)
.sorted()
.forEach(System.out::println);
}
}
这篇关于Java8新特性-为什么使用Lambda表达式的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!