| public class TestLambda1 { |
| |
| @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); |
| } |
| |
| |
| @Test |
| public void test2(){ |
| Comparator<Integer> com = (x, y) -> Integer.compare(x, y); |
| TreeSet<Integer> ts = new TreeSet<>(com); |
| } |
| |
| } |
| # 实体类 |
| public class Employee { |
| |
| private int id; |
| |
| private String name; |
| |
| private int age; |
| |
| private double salary; |
| |
| public Employee() { |
| } |
| |
| public Employee(String name) { |
| this.name = name; |
| } |
| |
| public Employee(String name, int age) { |
| this.name = name; |
| this.age = age; |
| } |
| |
| public Employee(int id, String name, int age, double salary) { |
| this.id = id; |
| this.name = name; |
| this.age = age; |
| this.salary = salary; |
| } |
| |
| 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; |
| } |
| |
| public void setSalary(double salary) { |
| this.salary = salary; |
| } |
| |
| public String show() { |
| return "测试方法引用!"; |
| } |
| |
| @Override |
| public int hashCode() { |
| final int prime = 31; |
| int result = 1; |
| result = prime * result + age; |
| result = prime * result + id; |
| result = prime * result + ((name == null) ? 0 : name.hashCode()); |
| long temp; |
| temp = Double.doubleToLongBits(salary); |
| result = prime * result + (int) (temp ^ (temp >>> 32)); |
| return result; |
| } |
| |
| @Override |
| public boolean equals(Object obj) { |
| if (this == obj) |
| return true; |
| if (obj == null) |
| return false; |
| if (getClass() != obj.getClass()) |
| return false; |
| Employee other = (Employee) obj; |
| if (age != other.age) |
| return false; |
| if (id != other.id) |
| return false; |
| if (name == null) { |
| if (other.name != null) |
| return false; |
| } else if (!name.equals(other.name)) |
| return false; |
| if (Double.doubleToLongBits(salary) != Double.doubleToLongBits(other.salary)) |
| return false; |
| return true; |
| } |
| |
| @Override |
| public String toString() { |
| return "Employee [id=" + id + ", name=" + name + ", age=" + age + ", salary=" + salary + "]"; |
| } |
| |
| } |
| |
| public class TestLambda1 { |
| |
| # 创建集合 |
| List<Employee> emps = Arrays.asList( |
| new Employee(101, "张三", 18, 9999.99), |
| new Employee(102, "李四", 59, 6666.66), |
| new Employee(103, "王五", 28, 3333.33), |
| new Employee(104, "赵六", 8, 7777.77), |
| new Employee(105, "田七", 38, 5555.55) |
| ); |
| |
| |
| public List<Employee> filterEmployeeAge(List<Employee> emps){ |
| List<Employee> list = new ArrayList<>(); |
| |
| for (Employee emp : emps) { |
| if(emp.getAge() <= 35){ |
| list.add(emp); |
| } |
| } |
| |
| return list; |
| } |
| |
| @Test |
| public void test3(){ |
| List<Employee> list = filterEmployeeAge(emps); |
| for (Employee employee : list) { |
| System.out.println(employee); |
| } |
| } |
| |
| |
| 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; |
| } |
| |
| @Test |
| public void test3(){ |
| List<Employee> list = filterEmployeeSalary(emps); |
| for (Employee employee : list) { |
| System.out.println(employee); |
| } |
| } |
| |
| } |
| # 接口 |
| public interface MyPredicate<T> { |
| public boolean test(T t); |
| } |
| |
| # 实现类 |
| public class FilterEmployeeForAge implements MyPredicate<Employee>{ |
| @Override |
| public boolean test(Employee t) { |
| return t.getAge() <= 35; |
| } |
| } |
| |
| # 实现类 |
| public class FilterEmployeeForSalary implements MyPredicate<Employee> { |
| @Override |
| public boolean test(Employee t) { |
| return t.getSalary() >= 5000; |
| } |
| } |
| |
| # 测试 |
| public class TestLambda1 { |
| |
| 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> list = filterEmployee(emps, new FilterEmployeeForAge()); |
| for (Employee employee : list) { |
| System.out.println(employee); |
| } |
| System.out.println("------------------------------------------"); |
| List<Employee> list2 = filterEmployee(emps, new FilterEmployeeForSalary()); |
| for (Employee employee : list2) { |
| System.out.println(employee); |
| } |
| } |
| |
| } |
| # 接口 |
| public interface MyPredicate<T> { |
| public boolean test(T t); |
| } |
| |
| # 测试 |
| public class TestLambda1 { |
| |
| 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 test5(){ |
| List<Employee> list = filterEmployee(emps, new MyPredicate<Employee>() { |
| @Override |
| public boolean test(Employee t) { |
| return t.getId() <= 103; |
| } |
| }); |
| for (Employee employee : list) { |
| System.out.println(employee); |
| } |
| } |
| |
| } |
| public class TestLambda1 { |
| |
| 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 test6(){ |
| List<Employee> list = filterEmployee(emps, (e) -> e.getAge() <= 35); |
| list.forEach(System.out::println); |
| System.out.println("------------------------------------------"); |
| List<Employee> list2 = filterEmployee(emps, (e) -> e.getSalary() >= 5000); |
| list2.forEach(System.out::println); |
| } |
| |
| } |
| public class TestLambda1 { |
| @Test |
| public void test7(){ |
| emps.stream() |
| .filter((e) -> e.getAge() <= 35) |
| .forEach(System.out::println) |
| System.out.println("----------------------------------------------") |
| emps.stream() |
| .map(Employee::getName) |
| .limit(3) |
| .sorted() |
| .forEach(System.out::println) |
| } |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术