java.util.function包详解-Lambda
您有任何问题或意见都可以在评论区回复哦,欢迎大家一起来讨论,共同学习进步
java.util.function包中有43个function interface,但是实际上只有四大类:
Consumers消费者
Supplier供应商
Functions功能
Predicates谓词
1.Consumers一个对象,BiConsumer两个对象
实际都是对传入的T实体进行操作处理
public interface Consumer {
public void accept(T var1);
}Consumer printer = s-> System.out.println(s);
//或者
Consumer printer = System.out::println;
public interface BiConsumer {
void accept(T var1, U var2);
}
2.Supplier返回类型T的对象,没有任何入参 实际是创建了T实体并返回的操作
public interface Supplier {
T get();
}Supplier bankAccountSupplier = ()->new BankAccount();
//或者
Supplier bankAccountSupplier = BankAccount::new;
3.Function 实际上是对类型T实体进行相应的操作并返回类型为R的实体
public interface Function {
R apply(T var1);
}
Function amtFunction = bankAccount -> bankAccount.getBalance();
//或者
Function amtFunction = BankAccount::getBalance;
4.Predicate 确定实体T是否满足约束,返回boolean
public interface Predicate {
boolean test(T t);
}
Predicate test = bankAccount -> bankAccount.getBalance()>10;
//自定义
Predicate p = new Predicate() {
@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 apply(int value);
}
public interface LongToDoubleFunction {
double applyAsDouble(long value);
}
【java.util.function包详解-Lambda】可以自定义Predicate接口,标准验证以及自定义方法,静态方法等
@FunctionalInterface
public interface Predicate {
public boolean test(T t);
public default Predicate and(Predicate other){
return t -> test(t) && other.test(t);
}
public default Predicate or(Predicate other){
return t -> test(t) || other.test(t);
}
//静态方法第一种
public static Predicate isEqualsTo(String string){
return s -> s.equals(string);
}
//还可以写成另一种
public static Predicate isEqualsTo(U u){
return s -> s.equals(u);
}
}
public class Main {
public static void main(String[] args) {
Predicate p1 = s -> s.length()<20;
Predicate 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 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 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 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
}
}
推荐阅读
- 喂,你结婚我给你随了个红包
- CET4听力微技能一
- 放下心中的偶像包袱吧
- 社保代缴公司服务费包含哪些
- Beego打包部署到Linux
- 世界之大,包罗万象--|世界之大,包罗万象-- 读《我不过低配的人生》
- 用npm发布一个包的教程并编写一个vue的插件发布
- 积极探索|积极探索 绽放生命 ???——心心相印计划:青少年心理工作研讨小组全国大型公益行动第二次活动包头市青山区分校圆满成功
- 那个喝大了的女人在群里发了一晚上的红包
- HttpClient对外部网络的操作