当前位置:   article > 正文

JDK8新特性03-函数式接口

JDK8新特性03-函数式接口

目录

一、函数式接口由来

二、函数式接口

2.1Supplier

2.2Consumer

2.3Function

2.4Predicate


一、函数式接口由来

先看lambda表达式是怎么写的,demo:

  1. public class Demo01Function {
  2. public static void main(String[] args) {
  3. fun1((arr) -> {
  4. int sum = 0;
  5. for (int i : arr) {
  6. sum +=i;
  7. }
  8. return sum;
  9. });
  10. }
  11. public static void fun1(Operator operator){
  12. int[] arr = {1,2,3,4};
  13. int sum = operator.getSum(arr);
  14. System.out.println("sum=" + sum);
  15. }
  16. }
  1. /**
  2. * 函数式接口
  3. */
  4. @FunctionalInterface
  5. interface Operator{
  6. int getSum(int[] arr);
  7. }

发现,

1.我们要自己定义接口Operator,然而实际上lambda表达式中,我们并不关心函数式接口的接口名和

抽象方法名

2.lambda表达式只关心参数列表和返回值类型

因此,

jdk8中给我们提供了很多常用的函数式接口。

二、函数式接口

在JDK中帮我们提供的由函数式接口,主要在java.util,function包中

2.1Supplier

无参有返回值的接口

  1. @FunctionalInterface
  2. public interface Supplier<T> {
  3. /**
  4. * Gets a result.
  5. *
  6. * @return a result
  7. */
  8. T get();
  9. }

demo,SupplierTest:

  1. public class SupplierTest {
  2. public static void main(String[] args) {
  3. fun1(() -> {
  4. int arr[] = {22,44,32,87,22,67};
  5. //计算出数组中的最大值
  6. Arrays.sort(arr);
  7. return arr[arr.length - 1];
  8. });
  9. }
  10. private static void fun1(Supplier<Integer> supplier){
  11. //get()是一个无参有返回值的抽象方法
  12. Integer max = supplier.get();
  13. System.out.println("max=" + max);
  14. }
  15. }
运行输出:max=87

2.2Consumer

有参无返回值的接口,前面介绍的Supplier接口是生产数据的,而Consumer是消费数据的

jdk中源码:

  1. @FunctionalInterface
  2. public interface Consumer<T> {
  3. /**
  4. * Performs this operation on the given argument.
  5. *
  6. * @param t the input argument
  7. */
  8. void accept(T t);
  9. }

demo ConsumerTest:

  1. public class ConsumerTest {
  2. public static void main(String[] args) {
  3. fun1(msg -> {
  4. System.out.println(msg + "--转换为大写-->" + msg.toUpperCase());
  5. });
  6. }
  7. private static void fun1(Consumer<String> consumer){
  8. consumer.accept("HelloWorld");
  9. }
  10. }
运行输出:HelloWorld--转换为大写-->HELLOWORLD

默认方法 andThen:

如果一个方法的参数和返回值全部是Consumer类型,那么就可以实现效果,消费一个数据的时候,首先做一个操作然后再做一个操作实现组合,而这个方法就是Consumer接口中的default方法andThen方法

  1. public class ConsumerAndThenTest {
  2. public static void main(String[] args) {
  3. test(msg1 -> {
  4. System.out.println(msg1 + "--转换为大写-->" + msg1.toUpperCase());
  5. },msg2 -> {
  6. System.out.println(msg2 + "--转换为大写-->" + msg2.toLowerCase());
  7. });
  8. }
  9. private static void test(Consumer<String> c1,Consumer<String> c2){
  10. c1.andThen(c2).accept("HelloWorld");
  11. }
  12. }
运行输出:HelloWorld--转换为大写-->HELLOWORLD
          HelloWorld--转换为大写-->helloworld

2.3Function

有参有返回值的函数式接口,

Function接口时根据一中类型的数据得到另一种类型的数据,前置成为前置条件,后者成为后置条件。

 
  1. @FunctionalInterface
  2. public interface Function<T, R> {
  3. /**
  4. * Applies this function to the given argument.
  5. *
  6. * @param t the function argument
  7. * @return the function result
  8. */
  9. R apply(T t);
  10. }
  11. public class FunctionTest {
  12. public static void main(String[] args) {
  13. test(msg -> {
  14. int i = Integer.parseInt(msg);
  15. return i;
  16. });
  17. }
  18. //将String类型转换为Integer类型
  19. private static void test(Function<String,Integer> function){
  20. Integer apply = function.apply("666");
  21. System.out.println("apply=" + apply);
  22. }
  23. }
 

默认方法 andThen,也是用来进行组合操作的

  1. public class FunctionAndThenTest {
  2. public static void main(String[] args) {
  3. test(msg1 -> {
  4. return Integer.parseInt(msg1);
  5. },msg2 -> {
  6. return msg2 * 10;
  7. });
  8. }
  9. static void test(Function<String,Integer> f1, Function<Integer,Integer> f2){
  10. Integer apply = f1.andThen(f2).apply("666");
  11. System.out.println("apply:" + apply);
  12. }
  13. }

2.4Predicate

有参且返回值是布尔类型

  1. @FunctionalInterface
  2. public interface Predicate<T> {
  3. /**
  4. * Evaluates this predicate on the given argument.
  5. *
  6. * @param t the input argument
  7. * @return {@code true} if the input argument matches the predicate,
  8. * otherwise {@code false}
  9. */
  10. boolean test(T t);

使用:

  1. public class PredicateTest {
  2. public static void main(String[] args) {
  3. test((String msg) -> {
  4. return msg.length() > 3;
  5. },"hello");
  6. }
  7. private static void test(Predicate<String> predicate,String msg){
  8. boolean test = predicate.test(msg);
  9. System.out.println("b:" + test);
  10. }
  11. }
输出:b:true

再Predicate中提供了默认的逻辑关系操作:

  1. public class PredicateOtherMethodTest {
  2. public static void main(String[] args) {
  3. test(msg1 -> {
  4. return msg1.contains("H");
  5. },msg2 -> {
  6. return msg2.contains("W");
  7. });
  8. }
  9. private static void test(Predicate<String> p1,Predicate<String> p2){
  10. //与
  11. boolean hello1 = p1.and(p2).test("Hello");
  12. //或
  13. boolean hello2 = p1.or(p2).test("Hello");
  14. //非
  15. boolean hello3 = p1.negate().test("Hello");
  16. System.out.println("hello1:" + hello1);
  17. System.out.println("hello2:" + hello2);
  18. System.out.println("hello3:" + hello3);
  19. }
  20. }
输出:
hello1:false
hello2:true
hello3:false

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/53203
推荐阅读
  

闽ICP备14008679号