函数式编程

四大函数式接口

  • 四大原生函数式接口

    • 函数式接口:Function, 有一个输入和一个输出

    • 断定型接口:Predicate,有一个输入参数,返回值只能是布尔值

    • 消费型接口:Consumer,只有输入值,没有返回值

    • 供给型接口:Supplier,只有返回值

  • 只有一个抽象方法的接口,如Runnable,foreach(消费者类型的)

Function

  • 源码
@FunctionalInterface
public interface Function<T, R> {

    R apply(T t);

    default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
        Objects.requireNonNull(before);
        return (V v) -> apply(before.apply(v));
    }

    default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
        Objects.requireNonNull(after);
        return (T t) -> after.apply(apply(t));
    }

    static <T> Function<T, T> identity() {
        return t -> t;
    }
}
  • 例子
public class Test {
    public static void main(String[] args) {
        // Function 有一个输入和一个输出
        // 工具类:输出输入的值
        Function<String, String> function = new Function<>() {
            @Override
            public String apply(String str) {
                return str;
            }
        };

        // 用lambda表达式
        Function function1 = (str) -> {
            return str;
        };

        System.out.println(function.apply("Function"));
        System.out.println(function1.apply("Function"));
    }
}

Predicate

  • 源码
@FunctionalInterface
public interface Predicate<T> {

    boolean test(T t);

    default Predicate<T> and(Predicate<? super T> other) {
        Objects.requireNonNull(other);
        return (t) -> test(t) && other.test(t);
    }

    default Predicate<T> negate() {
        return (t) -> !test(t);
    }

    default Predicate<T> or(Predicate<? super T> other) {
        Objects.requireNonNull(other);
        return (t) -> test(t) || other.test(t);
    }

    static <T> Predicate<T> isEqual(Object targetRef) {
        return (null == targetRef)
                ? Objects::isNull
                : object -> targetRef.equals(object);
    }

    @SuppressWarnings("unchecked")
    static <T> Predicate<T> not(Predicate<? super T> target) {
        Objects.requireNonNull(target);
        return (Predicate<T>)target.negate();
    }
}
  • 例子
public class Test {
    public static void main(String[] args) {
        // 判断字符串是否为空
        Predicate<String> predicate = new Predicate<String>() {
            @Override
            public boolean test(String s) {
                return s.isEmpty();
            }
        };
        Predicate<String> predicate1 = (str) -> {
            return str.isEmpty();
        };
        Predicate<String> predicate2 = String::isEmpty;
        System.out.println(predicate.test("Predicate"));
        System.out.println(predicate1.test("Predicate"));
        System.out.println(predicate2.test("Predicate"));
    }
}

Consumer

  • 源码
@FunctionalInterface
public interface Consumer<T> {

    /**
     * Performs this operation on the given argument.
     *
     * @param t the input argument
     */
    void accept(T t);

    default Consumer<T> andThen(Consumer<? super T> after) {
        Objects.requireNonNull(after);
        return (T t) -> { accept(t); after.accept(t); };
    }
}
  • 例子
public class Test {
    public static void main(String[] args) {
        // 消费型接口只有输入没有返回值
        Consumer<String> consumer = new Consumer<>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        };

        Consumer<String> consumer1 = (str) -> {
            System.out.println(str);
        };

        Consumer<String> consumer2 = System.out::println;

        consumer.accept("Consumer");
        consumer1.accept("Consumer");
        consumer2.accept("Consumer");
        consumer.andThen(consumer1).andThen(consumer2).andThen(consumer).accept("Consumer-");
    }
}

Supplier

  • 源码
@FunctionalInterface
public interface Supplier<T> {

    /**
     * Gets a result.
     *
     * @return a result
     */
    T get();
}
  • 例子
public class Test {
    public static void main(String[] args) {
        Supplier<String> supplier = new Supplier<>() {
            @Override
            public String get() {
                return "Supplier";
            }
        };

        Supplier<String> supplier1 = () -> {
            return "Supplier";
        };

        Supplier<String> supplier2 = () -> "Supplier";

        System.out.println(supplier.get());
        System.out.println(supplier1.get());
        System.out.println(supplier2.get());
    }
}
posted @ 2021-12-20 20:32  n1ce2cv  阅读(29)  评论(0编辑  收藏  举报