Lambda提供的4个函数式接口

package com.company;

import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;

public class TestLambda {
static final List emps = Arrays.asList(
new Employee(21, “r”),
new Employee(20, “l”),
new Employee(18, “goal”),
new Employee(18, “l”)
);
static final List list = new ArrayList(emps);
/*
* 四个函数式接口 消费型/供给型/函数型/断言型
* */
public void client(Employee monney, Consumer consumer) {
consumer.accept(monney);
}

public List supply(Employee monney, Supplier supplier) {
    list.add(supplier.get());
    Iterator iterator = list.listIterator();
    while (iterator.hasNext()) {
        if (monney.getAge() != iterator.next().getAge()) {
            iterator.remove();
        }
    }
    return list;
}

public String apply(Employee monney, Function apply) {
    //JDK1.8新特性使用streamAPI将list转map
    Map maps =
            list.stream().collect
                    (Collectors.toMap(Employee::getAge, Employee::getName, (key1, key2) -> key1));
    maps.put(monney.getAge(), apply.apply(monney));//put覆盖将18=goal替换
    maps.put(18, apply.apply(monney));//注意put的覆盖(key值相同),将18=goal替换,可自定义MyHashMap extends HashMap
    Iterator iterator = maps.keySet().iterator();
    while (iterator.hasNext()) {
        if (monney.getAge() != iterator.next()) {
            iterator.remove();
        }
    }
    return maps.toString();
}

public List cut(List monney, Predicate cut) {
    List list = new ArrayList(emps);
    for (Employee e:
         monney) {
        if (cut.test(e)){
            list.remove(e);
        }
    }
    return list;
}

@org.junit.Test
public void test() {

    Collections.sort(emps, (e1, e2) -> {
        if (e1.getAge() == e2.getAge()) {
            return e1.getName().compareTo(e2.getName());
        }
        return -Integer.compare(e1.getAge(), e2.getAge());
    });
    emps.forEach((e) -> System.out.print(e.toString()));
    System.out.print("\n");

    //第一个值是啥就消费啥
    client(new Employee(18, "plmm"), r-> System.out.println("你消费了 " + r));

    //将第一个值作为比较条件,按需求供给
    supply(new Employee(18, "plmm"), () -> new Employee(21, "bp")).forEach(
            e -> System.out.println("你提供了" + e.toString())
    );
    //将第一个值作为比较条件,申请函数式消费
    System.out.println(apply(new Employee(18, "plmm"), s -> "你函数式消费了" + s));

    //将第二个值作为过滤条件,过滤
    System.out.println("按条件=18 的 remove 了后"+cut(list, s -> s.getAge() == 18));
    System.out.println("emps的final = " + emps);//emps的Arrays.alist无法remove,会抛出异常.全部新建list代替
    System.out.println("list的final = " + list);//在supply方法remove后就定值了
}

}

class Employee {
public int getAge() {
return age;
}

public void setAge(int age) {
    this.age = age;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

private int age;

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


private String name;

@Override
public String toString() {
    return "Employee{" +
            "age=" + age +
            ", name='" + name + '\'' +
            '}';
}

}

你可能感兴趣的:(Lambda)