java.util.function包详解-Lambda

发布于:2023-03-12 ⋅ 阅读:(47) ⋅ 点赞:(0)

您有任何问题或意见都可以在评论区回复哦,欢迎大家一起来讨论,共同学习进步

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接口,标准验证以及自定义方法,静态方法等

@FunctionalInterface
public 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 :false
p3 test 123456 :true
p3 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 :true
p4 test 123456 :true
p4 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 :true
p5 test NO :false
    }
}
本文含有隐藏内容,请 开通VIP 后查看