您有任何问题或意见都能够在评论区回复哦,欢送大家一起来探讨,独特学习提高

java.util.function包中有43个function interface,然而实际上只有四大类:
Consumers消费者
Supplier供应商
Functions性能
Predicates谓词

1.Consumers一个对象,BiConsumer两个对象

理论都是对传入的T实体进行操作解决

public interface Consumer<T> {    public void accept(T var1);}Consumer<String> printer = s-> System.out.println(s);//或者Consumer<String> printer = System.out::println;public interface BiConsumer<T, U> {    void accept(T var1, U var2);}

2.Supplier返回类型T的对象,没有任何入参

理论是创立了T实体并返回的操作

public interface Supplier<T> {    T get();}Supplier<BankAccount> bankAccountSupplier = ()->new BankAccount();//或者Supplier<BankAccount> bankAccountSupplier = BankAccount::new;

3.Function

实际上是对类型T实体进行相应的操作并返回类型为R的实体

public interface Function<T, R> {    R apply(T var1);}Function<BankAccount, Integer> amtFunction = bankAccount -> bankAccount.getBalance();//或者Function<BankAccount, Integer> amtFunction = BankAccount::getBalance;

4.Predicate

确定实体T是否满足束缚,返回boolean

public interface Predicate<T> {    boolean test(T t);}Predicate<BankAccount> test = bankAccount -> bankAccount.getBalance()>10;//自定义Predicate<String> p = new Predicate<String>() {    @Override    public boolean test(String s) {        return s.length()<20;    }};p = s -> s.length()<20;System.out.println(p.test("Hello 12321321321321312321321"));

5.还有一些其余的类型

intPredicate,intFunction,intConsumer,入参须要指定为int
以及intToDoubleFunction,入参为int,返回参数为double

public interface IntFunction<R> {    R apply(int value);}public interface LongToDoubleFunction {    double applyAsDouble(long value);}

能够自定义Predicate接口,规范验证以及自定义办法,静态方法等

@FunctionalInterfacepublic interface Predicate<T> {    public boolean test(T t);    public default Predicate<T> and(Predicate<T> other){        return t -> test(t) && other.test(t);    }    public default Predicate<T> or(Predicate<T> other){        return t -> test(t) || other.test(t);    }    //静态方法第一种    public static Predicate<String> isEqualsTo(String string){        return s -> s.equals(string);    }    //还能够写成另一种    public static <U> Predicate<U> isEqualsTo(U u){        return s -> s.equals(u);    }}public class Main {    public static void main(String[] args) {        Predicate<String> p1 = s -> s.length()<20;        Predicate<String> p2 = s -> s.length()>5;        boolean b= p1.test("Hello");        System.out.println("Hello is shorter than 20 chars is :"+b);输入:Hello is shorter than 20 chars is :true        Predicate<String> p3 = p1.and(p2);        System.out.println("p3 test 123 :"+p3.test("123"));        System.out.println("p3 test 123456 :"+p3.test("123456"));        System.out.println("p3 test 123456789012345678901 :"+p3.test("123456789012345678901"));输入:p3 test 123 :falsep3 test 123456 :truep3 test 123456789012345678901 :false        Predicate<String> p4 = p1.or(p2);        System.out.println("p4 test 123 :"+p4.test("123"));        System.out.println("p4 test 123456 :"+p4.test("123456"));        System.out.println("p4 test 123456789012345678901 :"+p4.test("123456789012345678901"));输入:p4 test 123 :truep4 test 123456 :truep4 test 123456789012345678901 :true        Predicate<String> p5 = Predicate.isEqualsTo("YES");        System.out.println("p5 test YES :"+p5.test("YES"));        System.out.println("p5 test NO :"+p5.test("NO"));输入:p5 test YES :truep5 test NO :false    }}