美文网首页
黑猴子的家:Java 8 -> Lambda 表达式进阶

黑猴子的家:Java 8 -> Lambda 表达式进阶

作者: 黑猴子的家 | 来源:发表于2019-03-02 06:01 被阅读2次
    1、需求

    问题:针对员工的集合数据,有如下的一些需求,我们考虑如何完成?
    需求1:获取当前公司中员工年龄大于30的员工信息
    需求2:获取公司中工资大于 5000 的员工信息

    2、创建员工实体类 Employee
    package  com.yinggu.domain;
    
     * @author   黑猴子的家
     * http://www.121ugrow.com/
    
    public class Employee {
    
        private int id;
        private String name;
        private int age;
        private double salary;
    
        public void setSalary(double salary) {
            this.salary = salary;
        }
    
        public Employee(int id,String name,int age,double salary) {
            super ();
            this.id = id;
            this.name = name;
            this.age = age;
            this.salary = salary;
        }
    
        public Employee() {
            super ();
        }
    
        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;
        }
    
        @Override
        public String toString() {
            return "Employee [id=" + id + 
                        ", name=" + name + 
                        ", age=" + age + 
                        ", salary=" + salary + "]";
        }
    }
    
    3、虚拟出多个员工数据
    package com.yinggu.data;
    import java.util.ArrayList;
    import java.util.List;
    import com.yinggu.domain.Employee;
    
     * 封装多个员工对象的数据类 
     * @author 黑猴子的家
     * https://www.jianshu.com/u/37fd8e2dff4c
    
    public class EmployeeData {
          public static List<Employee> getData() {
                List<Employee> list = new ArrayList<>();
                list.add(new Employee(1, "乔峰", 35, 29000));
                list.add(new Employee(2, "虚竹", 27, 39000));
                list.add(new Employee(3, "阿紫", 18, 8000));
                list.add(new Employee(4, "阿朱", 26, 18000));
                list.add(new Employee(5, "段誉", 23, 45000));
                list.add(new Employee(6, "王语嫣", 21, 1000));
                return list;
          }
    }
    
    4、抽象类
    package com.yinggu.util;
    
     * @author 黑猴子的家
     * http://www.121ugrow.com/
    
    public interface Filter<T> {
          boolean filter(T e);
    }
    
    5、实现需求方式一 --> java 方法
    package com.yinggu.demo1;
    import java.util.ArrayList;
    import java.util.List;
    import org.junit.Test;
    import com.yinggu.data.EmployeeData;
    import com.yinggu.domain.Employee;
    import com.yinggu.util.Filter;
    
     * 此类用于演示Lambda表达式使用二
     * @author 黑猴子的家
     * https://www.jianshu.com/u/37fd8e2dff4c
     * 问题:针对员工的集合数据,有如下的一些需求,我们考虑如何完成?
     *         需求1:获取当前公司中员工年龄大于30的员工信息 
     *         需求2:获取公司中工资大于 5000 的员工信息 
    
    public class TestLambda2 {
          // 进阶1
          @Test
          public void test1() {
                List<Employee> list = EmployeeData.getData();
                // 需求1:获取当前公司中员工年龄大于30的员工信息
                List<Employee> ageList = filterByAge(list);
                printList(ageList);
                System.out.println("--------------------------------");
                // 需求2:获取当前公司中员工工资>20000的员工信息
                List<Employee> salaryList = filterBySalary(list);
                printList(salaryList);
          }
    
          // 根据过滤器进行筛选员工信息 的方法
          public List<Employee> getData(List<Employee> list, Filter<Employee> filter) {
                List<Employee> data = new ArrayList<>();
                for (Employee employee : list) {
                      if (filter.filter(employee)) {
                            data.add(employee);
                      }
                }
                return data;
          }
    
          // 打印集合对象的各个元素的方法
          public void printList(List<Employee> list) {
                for (Employee employee : list) {
                      System.out.println(employee);
                }
          }
    
          /*
           * 按年龄过滤,返回符和要求的数据的方法
           */
          public List<Employee> filterByAge(List<Employee> list) {
                List<Employee> data = new ArrayList<>();
                for (Employee employee : list) {
                      if (employee.getAge() > 30) {
                            data.add(employee);
                      }
                }
                return data;
          }
    
          /*
           * 按工资过滤,返回符和要求的数据的方法
           */
          public List<Employee> filterBySalary(List<Employee> list) {
                List<Employee> data = new ArrayList<>();
                for (Employee employee : list) {
                      if (employee.getSalary() > 20000) {
                            data.add(employee);
                      }
                }
                return data;
          }
    }
    
    6、实现需求方式二 --> 匿名内部类
    进阶2
    @Test
    public void test2() {
          List<Employee> list = EmployeeData.getData();
          // 需求1:获取当前公司中员工年龄大于30的员工信息
          Filter<Employee> ageFilter = new Filter<Employee>() {
                @Override
                public boolean filter(Employee e) {
                      return e.getAge() > 30;
                }
          };
          List<Employee> ageList = getData(list, ageFilter);
          printList(ageList);
          System.out.println("--------------------------------");
          // 需求2:获取当前公司中员工工资>20000的员工信息
          Filter<Employee> salaryFilter = new Filter<Employee>() {
                @Override
                public boolean filter(Employee e) {
                      return e.getSalary() > 20000;
                }
          };
          List<Employee> salaryList = getData(list, salaryFilter);
          printList(salaryList);
    }
    
    7、实现需求方式三 --> Lambda
    // 进阶3
    @Test
    public void test3() {
          List<Employee> list = EmployeeData.getData();
          // 需求1:获取当前公司中员工年龄大于30的员工信息
          List<Employee> ageList = getData(list, e -> e.getAge() > 30);
          printList(ageList);
          System.out.println("--------------------");
          // 需求3:获取当前公司中员工工资>20000的员工信息
          List<Employee> salaryList = getData(list, e -> e.getSalary() > 20000);
          printList(salaryList);
    }
    

    相关文章

      网友评论

          本文标题:黑猴子的家:Java 8 -> Lambda 表达式进阶

          本文链接:https://www.haomeiwen.com/subject/ltfjuqtx.html