四大函数式接口

四大函数式接口

新时代程序员必会:lambda表达式,链式编程,函数式接口, Stream流式计算

函数式接口:只有一个函数的接口. Example: Runnable, Callable都是常用的函数式接口

这四大函数式接口可以在javase文档中查看,java.util.function包下

Function 函数型接口

Predicate 判定型接口

Consumer 消费型接口

Supplier 供给型接口

函数型接口:接口输入一个是函数的输入一个是函数的输出

@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);
    // ... 两个默认函数和一个静态函数
}
package com.example.juc;

import java.util.function.Function;

public class TestFunction {
    public static void main(String[] args) {
//        Function fun = new Function() {
//            @Override
//            public Object apply(Object str) {
//                return str;
//            }
//        };
//        System.out.println(fun.apply("Happy"));

        Function fun = (str) -> str;
        System.out.println(fun.apply("Happy"));


    }
}

判定型接口:接口输入是函数的输入 函数的返回是一个布尔值

public interface Predicate<T> {

    /**
     * Evaluates this predicate on the given argument.
     *
     * @param t the input argument
     * @return {@code true} if the input argument matches the predicate,
     * otherwise {@code false}
     */
    boolean test(T t);
    // ...三个默认函数和一个静态函数
}
package com.example.juc;

import java.util.function.Predicate;

public class TestPredicate {
    public static void main(String[] args) {
//        Predicate<String> pre = new Predicate<String>() {
//            @Override
//            public boolean test(String str) {
//                return str.isEmpty();
//            }
//        };
//        System.out.println(pre.test("Happy"));

        Predicate<String> pre = (str) -> str.isEmpty();
        System.out.println(pre.test("Happy"));
    }
}

消费型接口:只有输入,没有输出

@FunctionalInterface
public interface Consumer<T> {

    /**
     * Performs this operation on the given argument.
     *
     * @param t the input argument
     */
    void accept(T t);
    // ...一个默认方法
}

供给型接口:只有输出,没有输入

@FunctionalInterface
public interface Supplier<T> {

    /**
     * Gets a result.
     *
     * @return a result
     */
    T get();
}
package com.example.juc;


import java.util.function.Consumer;

public class TestConsumer {
    public static void main(String[] args) {
//        Consumer consumer = new Consumer<String>() {
//            @Override
//            public void accept(String str) {
//                System.out.println(str);
//            }
//        };
//        consumer.accept("Happy");

        Consumer consumer = (str) -> {
            System.out.println(str);
        };
        consumer.accept("Happy");
    }
}
package com.example.juc;

import java.util.function.Supplier;

public class TestSupplier {
    public static void main(String[] args) {
        Supplier supplier = () -> {
            System.out.println("get");
            return 1024;
        };
        System.out.println(supplier.get());
    }
}
posted @ 2021-12-07 11:31  Oh,mydream!  阅读(123)  评论(0)    收藏  举报