匿名类-lambda-Stream API(学习笔记)
package com.atguigu.java8;
// 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.*;
// 实现方法
public class TestLambda {
// 原来的匿名内部类
@Test
public void test1(){
Comparator com = new Comparator() {
@Override
public int compare(Integer o1, Integer o2) {
return Integer.compare(o1,o2);
}
};
TreeSet ts = new TreeSet<>(com);
}
// Lambda 表达式
@Test
public void test2(){
Comparator com = (x,y) -> Integer.compare(x,y);
TreeSet ts = new TreeSet<>(com);
}
List 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 list = filterEmployees(emplyees);
for (Employee employee : list) {
System.out.println(employee);
}
}
public List filterEmployees(List list){
List emps = new ArrayList<>();
for (Employee employee : list) {
if (employee.getAge() >= 35) {
emps.add(employee);
}
}
return emps;
}
// 优化方式一:策略设计模式
@Test
public void test4(){
List list = filterEmployee(emplyees,new FilterEmployeeBySalary());
for (Employee employee : list){
System.out.println(employee);
}
System.out.println("=============================");
List list2 = filterEmployee(emplyees,new FilterEmployeeByAge());
for (Employee employee : list2) {
System.out.println(employee);
}
}
public List filterEmployee(List list, Mypredicate mp){
List emps = new ArrayList<>();
for (Employee employee : list) {
if (mp.test(employee)){
emps.add(employee);
}
}
return emps;
}
// 优化方二; 匿名内部类
@Test
public void test5(){
List list = filterEmployee(emplyees, new Mypredicate() {
@Override
public boolean test(Employee employee) {
return employee.getSalary() >= 1200;
}
});
for (Employee employee : list) {
System.out.println(employee);
}
}
// 优化方式三: Lambda 表达式
@Test
public void test6(){
List 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;
// 实现接口
public interface Mypredicate {
public boolean test(T t);
}
// Mypredicate 实现类1
package com.atguigu.java8;
public class FilterEmployeeByAge implements Mypredicate {
@Override
public boolean test(Employee employee) {
return employee.getAge() >= 35;
}
}
// Mypredicate 实现类2
package com.atguigu.java8;
public class FilterEmployeeBySalary implements Mypredicate{
@Override
public boolean test(Employee employee) {
return employee.getSalary() >= 400;
}
}