• 函数式接口 ( Java 8新特性 )


    函数式接口 ( 什么是函数式接口 )

    1. 任何接口,如果只包含唯一 一个抽象方法,那么它就是一个函数式接口。比如多线程中的Runnable接口,只包含一个run()抽象方法,所以Runnable接口是一个函数式接口。

      @FunctionalInterface
      public interface Runnable {
         
          public abstract void run();
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5

    注1:@FunctionalInterface 注解是 Java 8为函数式接口引入的新注解。@FunctionalInterface主要用于编译期错误检查,如果标注了该注解的接口不符合函数式接口的定义,那么就会编译不通过。

    蔡栋
    蔡栋


    注2:函数式接口有且仅有一个抽象方法,但可以有其它非抽象方法

    蔡栋


    注3:@FunctionalInterface注解不是必须的,如果一个接口符合 函数式接口 定义,那么加不加该注解都没有影响。加上该注解能够更好地让编译器进行检查。如果编写的不是函数式接口,但是加上了@FunctionInterface,那么编译器会报错。


    1. 对于函数式接口,我们可以使用 java 8提供的新特性 Lambda表达式来创建该接口的对象。

      //匿名内部类创建线程
      new Thread(new Runnable() {
          @Override
          public void run() {
              System.out.println("$*****$");
          }
      }).start();
      
          ||
          ||
          \/
      
      //lambda表达式创建线程
      new Thread(() -> System.out.println("lambda表达式")).start();
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14

    四大函数式接口:Function函数型接口、Predicate断定型接口、Consumer消费型接口、Supplier供给型接口

    • Consumer消费型接口 和 Supplier供给型接口是相对应的

    Function 函数型接口:传入一个 T 类型的参数,返回一个 R 类型的结果

    源码:

    /**
     * Represents a function that accepts one argument and produces a result.
     *
     * 

    This is a functional interface * whose functional method is {@link #apply(Object)}. * * @param the type of the input to the function * @param the type of the result of the function * * @since 1.8 */ @FunctionalInterface public interface Function<T, R> { /** * Applies this function to the given argument. * * @param t the function argument * @return the function result */ R apply(T t); }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    使用:接收一个传入参数,并返回一个结果的操作。

    import java.util.function.Function;
    
    public class Function_01 {
    
        public static void main(String[] args) {
            // 接受一个参数并产生结果的函数
            Function<String, String> function = (str)->{ return str; };
            System.out.println(function.apply("Function 的函数式接口"));
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    Predicate 断定型接口:有个一传入参数,返回类型是 boolean 值

    源码:

    /**
     * Represents a predicate (boolean-valued function) of one argument.
     *
     * 

    This is a functional interface * whose functional method is {@link #test(Object)}. * * @param the type of the input to the predicate * * @since 1.8 */ @FunctionalInterface public interface Predicate<T> { /** * Evaluates this predicate on the given argument. // 传入一个参数,返回一个 boolean 值 * * @param t the input argument * @return {@code true} if the input argument matches the predicate, * otherwise {@code false} */ boolean test(T t); }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    使用:传入一个参数,返回一个boolean值。

    public class Function_01 {
    
        public static void main(String[] args) {
            // 传入一个参数,返回一个 boolean 值
            Predicate<Integer> predicate = (num)->{ return num == 100; };
            System.out.println(predicate.test(99));
            
            Predicate<String> predicate = (str)->{ return str.isEmpty(); };
            System.out.println(predicate.test("哈哈"));
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    Consumer 消费型接口:传入一个 T 类型的参数,不返回结果

    源码:

    /**
     * Represents an operation that accepts a single input argument and returns no
     * result. Unlike most other functional interfaces, {@code Consumer} is expected
     * to operate via side-effects.
     *
     * 

    This is a functional interface * whose functional method is {@link #accept(Object)}. * * @param the type of the input to the operation * * @since 1.8 */ @FunctionalInterface public interface Consumer<T> { /** * Performs this operation on the given argument. //对给定参数进行操作 * * @param t the input argument */ void accept(T t); }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    使用:

    public class Function_01 {
    
        public static void main(String[] args) {
            // 接受一个参数,并且不返回结果的函数
            Consumer<String> consumer = (str)->{ System.out.println("Consumer 消费者函数式接口" + " 参数:" + str); };
            consumer.accept("00000");
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    Supplier 供给型接口:不传入参数,返回一个 T 类型的结果

    源码:

    /**
     * Represents a supplier of results.
     *
     * 

    There is no requirement that a new or distinct result be returned each * time the supplier is invoked. * *

    This is a functional interface * whose functional method is {@link #get()}. * * @param the type of results supplied by this supplier * * @since 1.8 */ @FunctionalInterface public interface Supplier<T> { /** * Gets a result. * * @return a result */ T get(); }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    使用:

    public class Function_01 {
    
        public static void main(String[] args) {
    	    // 返回一个指定类型的结果
            Supplier<Integer> supplier = () -> {return 10 >> 1;};
            System.out.println(supplier.get());
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
  • 相关阅读:
    22071系统移植day5
    抓包day1
    idea 模板参数注释 {@link}
    Apache Kafka 基于 S3 的数据导出、导入、备份、还原、迁移方案
    《Head First HTML与CSS》学习笔记
    VUE+ts项目配置--alias别名配置
    单例模式学习
    java开发之个人微信助手的开发
    java毕业设计InHome阅读平台mybatis+源码+调试部署+系统+数据库+lw
    C语言的冒泡排序
  • 原文地址:https://blog.csdn.net/weixin_42950079/article/details/126326673