Java8新特性—四大内置函数式接口( 三 )

供给型接口——Supplier接口 , 看下图源码:

Java8新特性—四大内置函数式接口

文章插图
由源码我们可以看出,Supplier接口只有一个待实的get方法,属于无参有返回值的抽象方法 。下面看一个例子:
public static void main(String[] args) {// 生成一个字符串Supplier<String> supplier1 = () -> "abcde";// 生成一个随机数Supplier<Integer> supplier2 = () -> new Random().nextInt(10);// 产生一个运行时异常Supplier<RuntimeException> supplier3 = () -> new RuntimeException();System.out.println(supplier1.get());System.out.println(supplier2.get().intValue());System.out.println(supplier3.get());}执行结果:
abcde2java.lang.RuntimeExceptionSupplier接口在JDK中的应用
Java8新特性—四大内置函数式接口

文章插图
generate方法返回一个无限连续的无序流,其中每个元素由提供的供应商(Supplier)生成 。generate方法用于生成常量流和随机元素流 。看下面例子:
public static void main(String[] args) {// 生成随机数Stream.generate(() -> new Random().nextInt(10));stream.forEach(System.out::println);// 生成随机布尔流Stream.generate(() -> new Random().nextBoolean()).forEach(System.out::println);// 生成常量流Stream.generate(() -> "Hello World!").forEach(System.out::println);}执行结果:
251--- #略truefalsetrue--- #略Hello World!Hello World!Hello World!--- #略由于generate返回无限连续流,为了限制流中元素的数量,我们可以使用Stream.limit方法
public static void main(String[] args) { Stream.generate(() -> new Random().nextInt(10)).limit(3).forEach(e -> System.out.println(e)); Stream.generate(() -> new Random().nextBoolean()).limit(3).forEach(e -> System.out.println(e)); Stream.generate(() -> "Hello World!").limit(3).forEach(e -> System.out.println(e));}执行结果:
363truefalsefalseHello World!Hello World!Hello World!4.Predicate接口什么是Predicate接口?
Java8新特性—四大内置函数式接口

文章插图
Predicate接口又称为断言型接口,test()方法有参但是返回值类型是固定的boolean,看下面例子:
public static void main(String[] args) {Predicate<String> predicate = (s) -> s.length() > 0;// 测试字符串的长度是否>0System.out.println(predicate.test("hello"));// 结果取反System.out.println(predicate.negate().test("hello"));System.out.println("=====or / and======");System.out.println(predicate.test(""));// 增加或判断,二者满足其一则为trueSystem.out.println(predicate.or(s -> s.equals("")).test(""));// 增加与判断,二者都满足则为trueSystem.out.println(predicate.and(s -> s.equals("hello")).test(""));System.out.println(predicate.and(s -> s.equals("hello")).test("hello"));System.out.println("=====isEqual======");// 判断是否相等System.out.println(Predicate.isEqual("hello").test(""));System.out.println(Predicate.isEqual("hello").test("hello"));Predicate<Boolean> nonNull = Objects::nonNull;Predicate<Boolean> isNull = Objects::isNull;Predicate<String> isEmpty = String::isEmpty;Predicate<String> isNotEmpty = isEmpty.negate();}执行结果:
truefalse=====or / and======falsetruefalsetrue=====isEqual======falsetruePredicate接口在JDK中的应用
Java8新特性—四大内置函数式接口

文章插图
Stream中的filter方法,用来过滤不满足条件的元素,使用Predicate传入过滤条件 。看下面例子:
Stream<Integer> stream = Stream.of(-1, 0, 1, 2, 3, 4);stream.filter(s -> s > 0).forEach(System.out::println);执行结果:
1234总结关于JDK1.8为什么要新增这四大内置函数式接口,其实就是Java的开发者将常用于代码的一些普遍场景抽象出来成为接口,而我们可以根据实际业务需求,实现这些接口的具体逻辑 。通过lambda表达式的方式,也可以使得代码更加简洁 。JDK中的函数式接口还有很多 , 但基本都是在四大函数式接口的基础之上加以拓展,有兴趣的童鞋可以自行研究 。
四大函数式接口的比较
函数式接口对应程序逻辑的抽象具体场景Function程序中映射逻辑的抽象比如我们写得很多的函数:接收入参,返回出参,方法代码块就是一个映射的具体逻辑 。Predicate程序中判断逻辑的抽象比如各种if判断,对于一个参数进行各种具体逻辑的判定,最后返回一个if else能使用的布尔值Consumer程序中的消费型逻辑的抽象就比如Collection体系的ForEach方法,将每一个元素取出,交给Consumer指定的消费逻辑进行消费Suppiler程序中的生产逻辑的抽象就比如最常用的,new对象,这就是一个很经典的生产者逻辑,至于new什么,怎么new,这就是Suppiler中具体逻辑的写法了参考资料

推荐阅读