[WIP]Java Functional Interface and Lamda Expression

创建: 2020/11/27

完成: 2020/11/27

更新: 修改标题 [Java Functional Interface] -> [[WIP]Java Functional Interface and Lamda Expression]

        时间不够,增加一篇reference

 

 

https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html

Functional interfaces provide target types for lambda expressions and method references.

函数接口提供lambda表达式和函数变量的数据类型

java.util.function.*

主要接口
Runnable

java.lang

常用于Thread

public interface Runnable {
    public abstract void run();
}

 

Supplier<T>  没有参数返回一个值
public interface Supplier<T> {
    public abstract T get();
}

 

 
Consumer<T>  有一个参数没有返回值
public interface Consumer<T> {
    void accept<T t>;
    default Consumer<T> andThen(ConSumer<? super T> after) {
        Object.requireNoNull(requireNonNull);
        return (T t) -> { accept(t); after.accept(t); };
    }
}

 

Function<T, R>

 有一个参数一个返回值

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;
    }
}

 

 

Prediable<T>

 有一个参数返回一个boolean

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);
    }
}

 

 

   
Lamda Expression
 

https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html

https://www.baeldung.com/java-8-lambda-expressions-tips

   
   
   
   
   
   
posted @ 2020-11-27 03:28  懒虫哥哥  阅读(94)  评论(0编辑  收藏  举报