Java JUC并发之四大函数式接口 Functional Interface

十二、四大函数式接口(必须掌握)

程序员必备技能:

  • lambda表达式
  • 链式编程
  • 函数式接口 (有且只有一个方法)
  • Stream流式计算

函数式接口 Functional Interface : 只有一个方法的接口

@FunctionalInterface
public interface Runnable {
    /**
     * When an object implementing interface {@code Runnable} is used
     * to create a thread, starting the thread causes the object's
     * {@code run} method to be called in that separately executing
     * thread.
     * <p>
     * The general contract of the method {@code run} is that it may
     * take any action whatsoever.
     *
     * @see     java.lang.Thread#run()
     */
    public abstract void run(); // 只有一个方法
}
// 存在超级多函数式接口
// 简化编程模型,在新版本的框架底层大量应用
// foreach(消费者类型的函数式接口)

// foreach(消费者类型的函数式接口)

default void forEach(Consumer<? super T> action) {
    Objects.requireNonNull(action);
    for (T t : this) {
        action.accept(t);
    }
}
@FunctionalInterface
public interface Consumer<T> {
    /**
     * Performs this operation on the given argument.
     *
     * @param t the input argument
     */
    void accept(T t);

四大函数式接口

  • Function 函数型接口

    @FunctionalInterface
    public interface Function<T, R> { // 传入参数T ,返回参数R
    
        /**
         * Applies this function to the given argument.
         *
         * @param t the function argument
         * @return the function result
         */
        R apply(T t);
    //========================================================
        
    package com.liu.function;
    
    import java.util.function.Function;
    
    /**
     * Function 函数型接口 一个输入参数 一个输出参数
     *
     * 只要是函数式接口,就可以用lambda表达式简化
     */
    public class Demo01 {
        public static void main(String[] args) {
    
            // 工具类 输出输入的值
    /*        Function function = new Function<String,String>() {
                @Override
                public String apply(String str) {
                    return str;
                }
            };*/
    
            Function<String,String> function = (str)->{ return str; };
    
            System.out.println(function.apply("ybb"));
        }
    }
    
    
  • Predicate 断定型接口

    package com.liu.function;
    
    import java.util.function.Predicate;
    
    /**
     * Predicate 断定型接口 有一个输入参数 返回值只能为布尔值
     */
    public class Demo02 {
    
        public static void main(String[] args) {
    
    /*        // 判断字符串是否为空
            Predicate<String> predicate = new Predicate<String>() {
                @Override
                public boolean test(String str) {
                    return str.isEmpty();
                }
            };*/
    
            // 判断字符串是否为空
            Predicate<String> predicate = (str)-> {
                    return str.isEmpty();
            };
    
            System.out.println(predicate.test(""));
    
        }
    }
    
  • Consumer 消费型接口 只有输入,没有返回值

    package com.liu.function;
    
    import java.util.function.Consumer;
    
    /**
     * Consumer 消费型接口 只有输入,没有返回值
     */
    public class Demo03 {
    
        public static void main(String[] args) {
    
    /*        Consumer<String> consumer = new Consumer<>() {
                @Override
                public void accept(String str) {
    
                    System.out.println(str);
                }
            };*/
    
    
            Consumer<String> consumer = (str)-> {
                System.out.println(str);
            };
    
            consumer.accept("ybb");
        }
    }
    
  • Supplier 供给型接口

    package com.liu.function;
    
    import java.util.function.Supplier;
    
    /**
     * Supplier 供给型接口 没有输入参数,有返回值
     */
    public class Demo04 {
        public static void main(String[] args) {
    
    /*        Supplier supplier = new Supplier<String>() {
                @Override
                public String get() {
                    System.out.println("get()");
                    return "ybb";
                }
            };
            System.out.println(supplier.get());*/
    
            Supplier supplier = () -> {
                    System.out.println("get()");
                    return "ybb";
            };
    
            System.out.println(supplier.get());
        }
    }
    
posted @ 2021-07-15 17:31  夕立君  阅读(78)  评论(0编辑  收藏  举报