优化方式(java匿名类)

匿名类-lambda-Stream API(学习笔记)

package com.atguigu.java8;

/**
 * @version 1.0
 * @Author 罗宗苇
 * @Date 2021/10/4 21:33
 * @注释
 */
// Employee员工类
public class Employee {
    private String name;
    private int age;
    private double salary;

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", 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;
    }

    public Employee(String name, int age, double salary) {
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

    public Employee() {
    }
}

package com.atguigu.java8;

import org.testng.annotations.Test;

import java.util.*;

/**
 * @version 1.0
 * @Author 罗宗苇
 * @Date 2021/10/4 21:21
 * @注释
 */

// 实现方法
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> emplyees = Arrays.asList(
            new Employee("张三",18,1132),
            new Employee("李四",48,1232),
            new Employee("王五",18,3432),
            new Employee("赵六",45,1232)
    );

    // 需求:获取当前公司中员工年龄大于35的员工信息
    @Test
    public void test3(){
        List<Employee> list = filterEmployees(emplyees);
        for (Employee employee : list) {
            System.out.println(employee);
        }
    }

    public List<Employee> filterEmployees(List<Employee> list){
        List<Employee> emps = new ArrayList<>();

        for (Employee employee : list) {
            if (employee.getAge() >= 35) {
                emps.add(employee);
            }
        }
        return emps;
    }

    // 优化方式一:策略设计模式
    @Test
    public void test4(){
        List<Employee> list = filterEmployee(emplyees,new FilterEmployeeBySalary());
        for (Employee employee : list){
            System.out.println(employee);
        }

        System.out.println("=============================");

        List<Employee> list2 = filterEmployee(emplyees,new FilterEmployeeByAge());
        for (Employee employee : list2) {
            System.out.println(employee);
        }
    }

    public List<Employee> filterEmployee(List<Employee> list, Mypredicate<Employee> mp){
        List<Employee> emps = new ArrayList<>();

        for (Employee employee : list) {
            if (mp.test(employee)){
                emps.add(employee);
            }
        }
        return emps;
    }

    // 优化方二; 匿名内部类
    @Test
    public void test5(){
        List<Employee> list = filterEmployee(emplyees, new Mypredicate<Employee>() {
            @Override
            public boolean test(Employee employee) {
                return employee.getSalary() >= 1200;
            }
        });
        for (Employee employee : list) {
            System.out.println(employee);
        }
    }

    // 优化方式三: Lambda 表达式
    @Test
    public void test6(){
        List<Employee> list = filterEmployee(emplyees,(e) -> e.getSalary() >= 1200);
        list.forEach(System.out::println);
    }

    // 优化方式四;Stream API
    @Test
    public void test7(){
        emplyees.stream()
                .filter((e) -> e.getSalary() >= 1200)
                .limit(2)
                .forEach(System.out::println);
        System.out.println("------------------------------");

        emplyees.stream()
                .map(Employee::getName)
                .forEach(System.out::println);
    }
}

package com.atguigu.java8;

// 实现接口

/**
 * @version 1.0
 * @Author 罗宗苇
 * @Date 2021/10/5 9:30
 * @注释
 */
public interface Mypredicate <T> {
    public boolean test(T t);
}

// Mypredicate 实现类1
package com.atguigu.java8;
/**
 * @version 1.0
 * @Author 罗宗苇
 * @Date 2021/10/5 9:33
 * @注释
 */
public class FilterEmployeeByAge implements Mypredicate<Employee> {
    @Override
    public boolean test(Employee employee) {
        return employee.getAge() >= 35;
    }
}

// Mypredicate 实现类2
package com.atguigu.java8;
/**
 * @version 1.0
 * @Author 罗宗苇
 * @Date 2021/10/5 9:37
 * @注释
 */
public class FilterEmployeeBySalary implements  Mypredicate<Employee>{
    @Override
    public boolean test(Employee employee) {
        return employee.getSalary() >= 400;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值