package 任务十三__流式计算.公司;
import java.util.Objects;
/**
* @author ${范涛之}
* @Description
* @create 2021-12-01 22:19
*/
public class Person {
private String name;
private Integer age;
private double size;
private double salary;
public Person(){}
public Person(Integer age){
this.age = age;
}
public Person(String name, Integer age, double size, double salary) {
this.name = name;
this.age = age;
this.size = size;
this.salary = salary;
}
@Override
public boolean equals(Object o){
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person student = (Person) o;
return Double.compare(student.size,size) == 0 &&
Double.compare(student.salary,salary) == 0 &&
Objects.equals(name,student.name) &&
Objects.equals(age,student.age);
}
@Override
public String toString() {
final StringBuffer sb = new StringBuffer("Person{");
sb.append("name='").append(name).append('\'');
sb.append(", age=").append(age);
sb.append(", size=").append(size);
sb.append(", salary=").append(salary);
sb.append('}');
return sb.toString();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public double getSize() {
return size;
}
public void setSize(double size) {
this.size = size;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
List<Person> studentList = Arrays.asList(
new Person("无极",18,188,35000),
new Person("范涛之",30,188,45000),
new Person("张荣卡",29,188,35000),
new Person("秦舒下",24,188,45000),
new Person("妹妹",32,188,43000)
);
/**
* 需求一:工资超过25000
* @param list
* @return
*/
public List<Person> fillterSudent1(List<Person> list){
List<Person> result = new ArrayList<>();
for (Person student :list){
if (student.getSalary() >25000){
result.add(student);
}
}
return result;
}
/**
* 需求二:身高超过一米七
*/
public List<Person> fillterSudent2(List<Person> list){
List<Person> result = new ArrayList<>();
for (Person student :list){
if (student.getSize()>170){
result.add(student);
}
}
return result;
}
myPredicate.test(student)
/**
* 优化方案1:策略模式+匿名内部类
*/
public List<Person> fillterSudent3(List<Person> list,MyPredicate<Person> myPredicate){
List<Person> result = new ArrayList<>();
for (Person student :list){
if (myPredicate.test(student)){
result.add(student);
}
}
return result;
}
public void test3(){
List<Person> list = fillterSudent3(studentList, new MyPredicate<Person>() {
@Override
public boolean test(Person o) {
return o.getSalary()>25000;
}
});
for (Person student:list){
System.out.println(student);
}
}
/**
* 继续优化
*/
public void test4(){
List<Person> list = fillterSudent3(studentList,(x)->x.getSalary()>25000);
for (Person student:list){
System.out.println(student);
}
}
/**
* 优化方案3:stream api
*/
@Test
public void test5() {
studentList.stream()
.filter((x) -> x.getSalary() > 25000)
.filter((x) -> x.getAge() > 25)
.limit(2)
.forEach(System.out::println);
System.out.println("------------------------");
Java8中引入了一个新的操作符"->”该操作符称为箭头操作符或Lambda操作符
箭头操作符将Lambda衣达式拆分成两部分:
左侧: L ambda衣达式的参数列表
右侧: Lambda表达式中所需执行的功能,即Lambda体
依赖于函数式接口,Lambda衣达式即对接口的实现
public void test1(){
Runnable runnable = new Runnable() {
@Override
public void run() {
System.out.println("1");
}
};
}
public void test1(){
Runnable runnable = () -> {
System.out.println("1");
};
}
public void test2(){
Consumer<String> com = (x) -> {
System.out.println(x);
};
com.accept("办公室乱死了烦!!");
}
@Test
public void test3(){
Consumer<String> com = x-> {
System.out.println(x);
};
com.accept("真的烦死了能不能声音小点!!!");
}
/**
* 语法四:两个参数以上,有返回值,并且lambda中有多条语句
*/
public void test4(){
Comparator<Integer> com =(x,y) ->{
System.out.println("111");
return Integer.compare(x,y);
};
}
/**
* 语法五:lambda体中只有一条语句,return 和大括号都可以省略不写
*/
public void test5(){
Comparator<Integer> com =(x,y) -> Integer.compare(x,y);
}
myPredicate.test(student)
这个东西,我们自己新建了一个接口用来接受一系列筛选条件,我这里再说明一个java自带的为了方面这么做的方法:Predicate
Consumer
/**
* 消费型Consumer 需求:传入一个参数做业务处理,不需要返回值
*/
public void happy(double money, Consumer<Double> con){
con.accept(money);
}
@Test
public void test1(){
happy(1000,(m)-> System.out.println("范涛之去洗脚花费了"+m+"元"));
}
Supplier
/**
* 供给型接口:Supplier 需求:产生指定数量的整数,放到集合中,返回集合
*/
public List<Integer> getNumList(int num, Supplier<Integer> sp){
List<Integer> result = new ArrayList<>();
for (int i = 0; i <num; i++) {
result.add(sp.get());
}
return result;
}
@Test
public void test2(){
List<Integer> NumList = getNumList(5,()->(int)(Math.random() * 100));
System.out.println(NumList);
}
/**
* 函数型 传入一个字符串,返回一个新的字符串
*/
public String strHander(String str , Function<String ,String> fun){
return fun.apply(str);
}
public void test3(){
String result = strHander("武林盟主:",(x)->x+"范涛之");
System.out.println(result);
}
/**
* 断言型
*/
public List<Person> filterStudent3(List<Person> list, Predicate<Person> myPredicate){
List<Person> result = new ArrayList<>();
for (Person student :list){
if (myPredicate.test(student)){
result.add(student);
}
}
return result;
}
List<Employee> result = list.stream()
.flatMap(company->company.getEmployees().stream())
// .filter(employee -> employee.getType().equals("OFFICER"))
.filter(employee -> employee.getAge()>20)
.filter(employee -> employee.isMarried())
.filter(employee -> employee.getType()== Employee.Type.MANAGER)
.sorted(Comparator.comparing(Employee::getAge))
.collect(Collectors.toList());
for (Employee employee:result){
System.out.println(employee);
}
/**
* 公司流
*/
// List result = list.stream()
// .flatMap(company -> company.getEmployees().stream())
// .filter(employee -> !employee.isMarried())
// .sorted(Comparator.comparing(Employee::getAge))
// .collect(Collectors.toList());
//
// for (Employee emp :result){
// System.out.println(emp.toString());
// }
// List result = list.stream()
// .filter(company -> company.getType()==(Company.Type.SMALL))
// .flatMap(company ->company.getEmployees().stream())
// .filter(employee -> employee.getAge()>20)
// .filter(employee -> employee.isMarried())
// .filter(employee -> employee.getType()== Employee.Type.MANAGER)
// .sorted(Comparator.comparing(Employee::getAge))
// .collect(Collectors.toList());
// for (Employee employee:result){
// System.out.println(employee);
// }
// List> result = list.stream()
// .filter(company -> company.getType()== Company.Type.BIG)
// .collect(Collectors.toList());
//
// for (Company company:result){
// System.out.println(result);
// }
/**
* 创建公司流 然后为了去获取公司
*/
List<Company<Employee>> result1 = list.stream()
.filter(company -> company.getType()== Company.Type.BIG)
.collect(Collectors.toList());
for (Company company:result1){
System.out.println(result1);
}
/**
* 创建公司流 然后为了去获取员工
*/
List<Employee> result2 = list.stream()
.flatMap(company -> company.getEmployees().stream())
.filter(employee -> employee.isMarried())
.collect(Collectors.toList());
/**
* 创建员工流 然后为了去获取员工
*/
List<Employee> result3 = employees.stream()
.filter(employee -> employee.isMarried())
.collect(Collectors.toList());
for (Employee employee:result3){
System.out.println(result3);
}