Java 8的Lambda學習

參考資料:https://www.dotnetperls.com/lambda-javajava

Lambdas用於建立函數對象。經過它們,咱們能夠在其它方法內部指定方法,甚至能夠把方法作爲參數傳遞給其它方法。Lambda須要有固定的定義,用以決定它的輸入參數和返回值,例如Supplier、Consumer、Function、Predicate和UnaryOperator的定義。
如下爲java.util.function包的幾個類學習app

環境:jdk1.8.0_144ide

1. java.util.function.Supplier函數

/**
 * 返回一個<T>類型的結果
 */
@FunctionalInterface
public interface Supplier<T> {

    /**
     * 獲取結果
     */
    T get();
}

應用舉例:學習

public class TestSupplier {
    private Supplier<Integer> supplier = new Supplier<Integer>() {
        @Override
        public Integer get() {
            return 100;
        }
    };
    private Supplier<Integer> supplierLambda = () -> 100;

    public int execute() {
        return execute(supplier);
    }

    public int executeLambda() {
        return execute(supplierLambda);
    }

    private int execute(Supplier<Integer> supplier) {
        return supplier.get();
    }
}

2. java.util.function.Consumerui

/**
 * 接收<T>類型的參數,無返回
 */
@FunctionalInterface
public interface Consumer<T> {

    /**
     * 基於輸入參數執行操做
     */
    void accept(T t);

    /**
     * 返回按順序執行的{@code Consumer}。任意操做的異常都會傳播到調用者,若當前操做拋出異常,{@code after}操做將不執行
     */
    default Consumer<T> andThen(Consumer<? super T> after) {
        Objects.requireNonNull(after);
        return (T t) -> { accept(t); after.accept(t); };
    }
}

應用舉例:spa

public class TestConsumer {
    private int consumerRtn;
    private int consumerLambdaRtn;
    private Consumer<Integer> consumer = new Consumer<Integer>() {
        @Override
        public void accept(Integer t) {
            consumerRtn = t + 1;
        }
    };
    private Consumer<Integer> consumerLambda = t -> consumerLambdaRtn = t;

    public int execute(int input) {
        execute(consumer, input);
        return consumerRtn;
    }

    public int executeLambda(int input) {
        execute(consumerLambda, input);
        return consumerLambdaRtn;
    }

    private void execute(Consumer<Integer> consumer, int input) {
        consumer.accept(input);
    }
}

3. java.util.function.Functioncode

/**
 * 接收一個<T>類型參數,並返回<R>類型參數
 */
@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 TestFunction {
    private Function<String, Integer> function = new Function<String, Integer>() {
        @Override
        public Integer apply(String t) {
            return Integer.parseInt(t);
        }
    };
    private Function<String, Integer> functionLambda = Integer::parseInt;

    public int execute(String input) {
        return execute(function, input);
    }

    public int executeLambda(String input) {
        return execute(functionLambda, input);
    }

    private int execute(Function<String, Integer> function, String input) {
        return function.apply(input);
    }
}

4. java.util.function.Predicateblog

/**
 * 單參數的斷言
 */
@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);
    }
}

應用舉例:

public class TestPredicate {
    private Predicate<Integer> predicate = new Predicate<Integer>() {
        @Override
        public boolean test(Integer t) {
            return t > 100;
        }
    };
    private Predicate<Integer> predicateLambda = t -> t > 100;

    public void execute(List<Integer> list) {
        execute(predicate, list);
    }

    public void executeLambda(List<Integer> list) {
        execute(predicateLambda, list);
    }

    private void execute(Predicate<Integer> predicate, List<Integer> list) {
        list.removeIf(predicate);
    }
}

5. java.util.function.UnaryOperator

/**
 * 輸入輸出爲同一類型的特殊Function
 */
@FunctionalInterface
public interface UnaryOperator<T> extends Function<T, T> {

    /**
     * Returns a unary operator that always returns its input argument.
     *
     * @param <T> the type of the input and output of the operator
     * @return a unary operator that always returns its input argument
     */
    static <T> UnaryOperator<T> identity() {
        return t -> t;
    }
}

應用舉例:

public class TestUnaryOperator {
    private UnaryOperator<Integer> unaryOperator = new UnaryOperator<Integer>() {
        @Override
        public Integer apply(Integer t) {
            return t + 100;
        }
    };
    private UnaryOperator<Integer> unaryOperatorLambda = t -> t + 100;

    public void execute(List<Integer> list) {
        execute(unaryOperator, list);
    }

    public void executeLambda(List<Integer> list) {
        execute(unaryOperatorLambda, list);
    }

    private void execute(UnaryOperator<Integer> unaryOperator, List<Integer> list) {
        list.replaceAll(unaryOperator);
    }
}
相關文章
相關標籤/搜索