Lambda表达式的优势-创新互联

Lambda表达式的优势

1. 匿名内部类和Lambda的对比
//原来的匿名内部类
    @Test
    public void test1(){
        Comparator comparator = new Comparator() {

            @Override
            public int compare(Integer o1, Integer o2) {
                return Integer.compare(o1, o2);
            }
        };

        TreeSet ts = new TreeSet<>(comparator);
    }
//Lambda表达式
    @Test
    public void test2(){
        Comparator comparator = (x ,y) -> Integer.compare(x, y);
        TreeSet ts = new TreeSet<>(comparator);

    }
2. 用Lambda表达式优化代码
  • 需求1:获取当前公司中员工年龄大于35的员工信息
  • 需求2:获取当前公司中员工工资大于5000的员工信息

    在北湖等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都网站设计、成都做网站、外贸网站建设 网站设计制作定制网站开发,公司网站建设,企业网站建设,成都品牌网站建设,营销型网站建设,外贸营销网站建设,北湖网站建设费用合理。
    public class Employee {
    private String name;
    private int age;
    private double salary;
    
    public Employee() {
        super();
    }
    
    public Employee(String name, int age, double salary) {
        super();
        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 +
                '}';
    }
    }
    List employees = Arrays.asList(
            new Employee("张三", 18 ,9999.99),
            new Employee("李四", 38, 5555.99),
            new Employee("王五", 50, 6666.66),
            new Employee("赵六", 16, 3333.33),
            new Employee("田七", 8, 7777.77)
    );
    实现方式
    @Test
    public void test3(){
                //获取年龄大于35岁的员工信息
        List employeeList = filterEmployee(employees);
        System.out.println("年龄大于35岁的员工信息:");
        for (Employee employee : employeeList){
            System.out.println(employee);
        }
    
        //获取当前公司中员工工资大于5000的员工信息
        List employeeList1 = filterEmployees2(employees);
        System.out.println("工资大于5000的员工信息:");
        for (Employee employee : employeeList1){
            System.out.println(employee);
        }
    }
    
    获取当前公司中员工年龄大于35的员工信息
    public List filterEmployee(List employeeList){
        List emps = new ArrayList<>();
    
        for (Employee emp : employeeList){
            if (emp.getAge() >= 35){
                emps.add(emp);
            }
        }
        return emps;
    }
    
    获取当前公司中员工工资大于5000的员工信息
    public List filterEmployees2(List employeeList){
    
        ArrayList employees = new ArrayList<>();
        for (Employee employee : employeeList){
            if (employee.getSalary() >= 5000){
                employees.add(employee);
            }
        }
        return employees;
    }
    优化方案一:策略设计模式
    public interface MyPredicate {
    
    public boolean test(T t);
    }
    public class FilterEmployeeByAge implements MyPredicate{
    @Override
    public boolean test(Employee employee) {
        return employee.getAge() >= 35;
    }
    }
    public class FilterEmployeeBySalary implements MyPredicate{
    @Override
    public boolean test(Employee employee) {
        return employee.getSalary() >= 5000;
    }
    }
    @Test
    public void test4(){
        List employeeList = filterEmployee(employees, new FilterEmployeeByAge());
        System.out.println("年龄大于35岁的员工信息:");
        for (Employee employee : employeeList){
            System.out.println(employee);
        }
    
        System.out.println("工资大于5000的员工信息:");
        List employeeList1 = filterEmployee(this.employees, new FilterEmployeeBySalary());
    
        for (Employee employee : employeeList1){
            System.out.println(employee);
        }
    
    }
    public List filterEmployee(List employeeList, MyPredicate mp){
        ArrayList employees = new ArrayList<>();
    
        for (Employee employee : employeeList){
            if (mp.test(employee)){
                employees.add(employee);
            }
        }
        return employees;
    }
    优化方案二:匿名内部类
    public interface MyPredicate {
    
    public boolean test(T t);
    }
    @Test
    public void test5(){
        List employeeList = filterEmployee(employees, new MyPredicate() {
            @Override
            public boolean test(Employee employee) {
                return employee.getAge() >= 35;
            }
        });
        System.out.println("年龄大于35岁的员工信息:");
        for (Employee employee : employeeList){
            System.out.println(employee);
        }
    
        List employeeList1 = filterEmployee(employees, new MyPredicate() {
            @Override
            public boolean test(Employee employee) {
                return employee.getSalary() >= 5000;
            }
        });
        System.out.println("工资大于5000的员工信息:");
        for (Employee employee : employeeList1){
            System.out.println(employee);
        }
    }
    优化方案三:Lambda表达式
    public List filterEmployee(List employeeList, MyPredicate mp){
        ArrayList employees = new ArrayList<>();
    
        for (Employee employee : employeeList){
            if (mp.test(employee)){
                employees.add(employee);
            }
        }
        return employees;
    }
    @Test
    public void test6(){
        System.out.println("年龄大于35岁的员工信息:");
        List employeeList = filterEmployee(employees, (e) -> e.getAge() >= 35);
        employeeList.forEach(System.out::println);
    
        System.out.println("工资大于5000的员工信息:");
        List employeeList1 = filterEmployee(employees, (e) -> e.getSalary() >= 5000);
        employeeList1.forEach(System.out::println);
    }
    优化方案四:Lambda表达式和Stream API
    @Test
    public void test7(){
        System.out.println("年龄大于35岁的员工信息:");
        employees.stream()
                 .filter((e) -> e.getAge() >= 35)
                 .forEach(System.out::println);
    
        System.out.println("工资大于5000的员工信息:");
        employees.stream()
                 .filter((e) -> e.getSalary() >= 5000)
                 .forEach(System.out::println);
    }

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


分享题目:Lambda表达式的优势-创新互联
文章出自:http://azwzsj.com/article/jopoo.html