共计 2768 个字符,预计需要花费 7 分钟才能阅读完成。
您有任何问题或意见都能够在评论区回复哦,欢送大家一起来探讨,独特学习提高
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
}
}
正文完