JAVA8学习——深入浅出Lambda表达式(学习过程) (2)

默认方法既保证了新特性的添加,又保证了老版本的兼容

//如,Iterable 中的 forEach方法 public interface Iterable<T> { default void forEach(Consumer<? super T> action) { Objects.requireNonNull(action); for (T t : this) { action.accept(t); } } } ForEach方法详解

比较重要的是行为,//action行为,而不是数据

/** * Performs the given action for each element of the {@code Iterable} * until all elements have been processed or the action throws an * exception. Unless otherwise specified by the implementing class, * actions are performed in the order of iteration (if an iteration order * is specified). Exceptions thrown by the action are relayed to the * caller. * * @implSpec * <p>The default implementation behaves as if: * <pre>{@code * for (T t : this) * action.accept(t); * }</pre> * * @param action The action to be performed for each element * @throws NullPointerException if the specified action is null * @since 1.8 */ default void forEach(Consumer<? super T> action) { Objects.requireNonNull(action); for (T t : this) { action.accept(t); } } Consumer 类型详解

名字的由来:消费,只消费,没有返回值

/** * Represents an operation that accepts a single input argument and returns no * result. Unlike most other functional interfaces, {@code Consumer} is expected * to operate via side-effects.//接口本身是带有副作用的,会对传入的唯一参数进行修改 * * <p>This is a <a href="http://www.likecs.com/package-summary.html">functional interface</a> * whose functional method is {@link #accept(Object)}. * * @param <T> the type of the input to the operation * * @since 1.8 */ @FunctionalInterface public interface Consumer<T> { /** * Performs this operation on the given argument. * * @param t the input argument */ void accept(T t); /** * Returns a composed {@code Consumer} that performs, in sequence, this * operation followed by the {@code after} operation. If performing either * operation throws an exception, it is relayed to the caller of the * composed operation. If performing this operation throws an exception, * the {@code after} operation will not be performed. * * @param after the operation to perform after this operation * @return a composed {@code Consumer} that performs in sequence this * operation followed by the {@code after} operation * @throws NullPointerException if {@code after} is null */ default Consumer<T> andThen(Consumer<? super T> after) { Objects.requireNonNull(after); return (T t) -> { accept(t); after.accept(t); }; } } Lambda表达式的作用

Lambda表达式为JAVA添加了缺失的函数式编程特性,使我们能够将函数当做一等公民看待

在将函数作为一等公民的语言中,Lambda表达式的类型是函数,但是在JAVA语言中,lambda表达式是一个对象,他们必须依附于一类特别的对象类型——函数是接口(function interface)

迭代方式(三种)

外部迭代:(之前使用的迭代集合的方式,fori这种的)

List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); }

内部迭代: ForEach(完全通过集合的本身,通过函数式接口拿出来使用Customer的Accept来完成内部迭代)

List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8); list.forEach(i -> System.out.println(i));

第三种方式:方法引用(method reference)

List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8); list.forEach(System.out::println);

2019年12月29日00:07:05 要睡觉了。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpgwwf.html