参考资料:https://www.dotnetperls.com/lambda-java
Lambdas用于创建函数对象。通过它们,我们可以在其它方法内部指定方法,甚至可以把方法做为参数传递给其它方法。Lambda需要有固定的定义,用以决定它的输入参数和返回值,例如Supplier、Consumer、Function、Predicate和UnaryOperator的定义。
以下为java.util.function包的几个类学习
环境:jdk1.8.0_144
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.Consumer
/** * 接收<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); }; } }
应用举例:
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.Function
/** * 接收一个<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.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); } }
应用举例:
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); } }