Java 8 自带的函数接口
我们使用lambda在处理自己定义的业务时,需要自定义函数式接口,其实java8已经内置了常用的接口,这样我们在用的时候不要需要自己定义接口,根据需要选择符合自己业务逻辑的接口
接口 | 输入参数|返回值类型|说明
---|---|---|---|---
Predicate
Consumer
Function
Supplier
UnaryOperator
BiFunction
BinaryOperator
Predicate 断言
Predicate predicate = i -> i > 0;
log.info("1>0:{}", predicate.test(1));
log.info("-1>0:{}", predicate.test(-1));
// jdk 常用的地方,数组集合过滤
int nums[] = {1, 2, 3, -2, -3};
int[] ints = IntStream.of(nums).filter(i -> i > 0).toArray();
log.info("nums:{}", Arrays.toString(ints));
输出结果
19:43:21.733 [main] INFO cn.wyj.learn.lambada.Example5 - 1>0:true
19:43:21.739 [main] INFO cn.wyj.learn.lambada.Example5 - -1>0:false
19:43:21.748 [main] INFO cn.wyj.learn.lambada.Example5 - nums:[1, 2, 3]
Consumer 消费一个数据
Consumer consumer1 = s -> {
System.out.println(s);
};
//简化写法
Consumer consumer2 = System.out::println;
consumer1.accept("string1");
consumer2.accept("string2");
// jdk 常用地方
List list = new ArrayList<>(Arrays.asList("t1", "t2", "t3", "t4", "t5"));
list.forEach(System.out::println);
输出结果
string1
string2
t1
t2
t3
t4
t5
Function
Function function = a -> a + 1;
Integer apply = function.apply(1);
log.info("result:{}", apply);
// 常用操作
List example5List = new ArrayList<>();
example5List.add(new Example5("张三", 10));
example5List.add(new Example5("李四", 18));
example5List.add(new Example5("王五", 20));
List collect = example5List.stream().map(a -> a.getName()).collect(Collectors.toList());
//简写
List collect1 = example5List.stream().map(Example5::getName).collect(Collectors.toList());
log.info("collect:{}", Arrays.toString(collect.toArray()));
log.info("collect1:{}", Arrays.toString(collect1.toArray()));
输出结果
20:01:22.503 [main] INFO cn.wyj.learn.lambada.Example5 - result:2
20:01:22.512 [main] INFO cn.wyj.learn.lambada.Example5 - collect:[张三, 李四, 王五]
20:01:22.513 [main] INFO cn.wyj.learn.lambada.Example5 - collect1:[张三, 李四, 王五]
Supplier
Supplier supplier = () -> "supplier";
String s = supplier.get();
log.info("supplier.get:{}", s);
Supplier supplierExample5 = Example5::new;
log.info("=============================");
Example5 a = supplierExample5.get();
Example5 b = supplierExample5.get();
log.info("a==b:{}", a == b);
log.info(" supplierExample5 :{}", a);
log.info(" supplierExample5 :{}", b);
Supplier supplierExample6 = () -> new Example5("张三", 20);
log.info("-----------------------------");
log.info("supplierExample6 :{}", supplierExample6.get());
//使用的地方,惰性加载
Supplier supplier1 = () -> {
log.info("延迟加载关联对象");
return "object";
};
log.info("-----------------------------");
String s1 = supplier1.get();
log.info("get:{}", s1);
输出结果
这里要注意的是:a==b:false supplier get到实例并不是单例的,而是多实例的,每次获取到的并不是同一个退休
20:41:58.666 [main] INFO cn.wyj.learn.lambada.Example5 - supplier.get:supplier
20:41:58.667 [main] INFO cn.wyj.learn.lambada.Example5 - =============================
20:41:58.667 [main] INFO cn.wyj.learn.lambada.Example5 - Example5()
20:41:58.667 [main] INFO cn.wyj.learn.lambada.Example5 - Example5()
20:41:58.667 [main] INFO cn.wyj.learn.lambada.Example5 - a==b:false
20:41:58.667 [main] INFO cn.wyj.learn.lambada.Example5 - supplierExample5 :Example5(name=null, age=0)
20:41:58.667 [main] INFO cn.wyj.learn.lambada.Example5 - supplierExample5 :Example5(name=null, age=0)
20:41:58.667 [main] INFO cn.wyj.learn.lambada.Example5 - -----------------------------
20:41:58.667 [main] INFO cn.wyj.learn.lambada.Example5 - Example5(String name, int age)
20:41:58.667 [main] INFO cn.wyj.learn.lambada.Example5 - supplierExample6 :Example5(name=张三, age=20)
20:41:58.667 [main] INFO cn.wyj.learn.lambada.Example5 - -----------------------------
20:41:58.667 [main] INFO cn.wyj.learn.lambada.Example5 - 延迟加载关联对象
20:41:58.667 [main] INFO cn.wyj.learn.lambada.Example5 - get:object
UnaryOperator
UnaryOperator unaryOperator = a1 -> a1 + 1;
Integer apply1 = unaryOperator.apply(10);
log.info("unaryOperator:{}", apply1);
BiFunction
BiFunction biFunction = (a2, b2) -> a2 + b2;
String apply2 = biFunction.apply("retulst:", 10);
log.info("apply2:{}", apply2);
BinaryOperator
BinaryOperator binaryOperator=(a3,b3)-> a3+b3;
String apply3 = binaryOperator.apply("test1", "test2");
log.info("apply3:{}", apply3);
写在最后话:
在Java中我们知道有八种数据类型,八种数据类型都有对应的包装类,自动装箱有一个问题,那就是在一个循环中进行自动装箱操作的情况,如下面的例子就会创建多余的对象,影响程序的性能。
如果我们不想程序频繁装箱拆箱,那么我们可以使用其对应的类来处理 例如: Function