java8函数式编程之三:集合中新增的方法:forEach
来源:互联网 发布:数据恢复实训 编辑:程序博客网 时间:2024/05/16 10:37
java 1.5的时候增加foreach的增强for循环,然而这里的forEach并不是那个foreach,该处谈论的foreach是集合类的父接口iterable中在jdk1.8新增加的foreach循环函数式方法改迭代方式也叫做内部迭代,内部迭代式通过集合本身的一些接口而不是通过指针去集合中取元素。先执行下面代码:
package com.test.java8;import java.util.ArrayList;import java.util.List;import java.util.function.Consumer;public class ForeachTest {public static void main(String[] args) {List<String> testList = new ArrayList<>();testList.add("a");testList.add("b");testList.add("c");testList.forEach(new Consumer<String>(){@Overridepublic void accept(String t) {System.out.println(t);}});}}
毫无疑问该方法会执行打印出集合中所有的元数。跟踪进入forEach函数中,发现该方法是定义在集合类的顶级接口Iterable<T>中:
/** * 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); } }
注意到该方法前面的关键字:default。default代表了该方法默认情况下可以被子类继承并使用,List接口间接继承自Iterable<T>接口,因此继承了该方法。而且,该方法实现在接口里,这里也是jdk1.8新增的特性,可以在接口里面定义实现方法,凡是在接口里的实现方法,叫做default 方法,这种方法一方面保证了新功能的加入,另一方面不修改该接口的实现类就可以使用该方法。该方法对每一个元素执行给定的action,也就是传值给action,action会一直执行直到所有的元素都执行完或者抛出异常才结束。对于consumer类:
@FunctionalInterfacepublic 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); }; }}可以看出也是一个函数式接口,代表了操作,该操作接收单独的输入参数,并且该操作不返回结果,不同于其他函数式接口,该方法可以修改接收到的参数。对于accept函数,接收一个值并且不返回值。
在函数式接口定义中:
* <p>Note that instances of functional interfaces can be created with * lambda expressions, method references, or constructor references.可以看出函数调用可以lambda表达式,方法引用,或者构造器引用,所以上面的开始的代码可以替换为:
public class ForeachTest {public static void main(String[] args) {List<String> testList = new ArrayList<>();testList.add("a");testList.add("b");testList.add("c");//testList.forEach(new Consumer<String>(){////@Override//public void accept(String t) {//System.out.println(t);//}//});//替换成:testList.forEach( t -> System.out.println(t));}}
可见,lambda表达式极大简化了匿名内部类的代码。另外,最后一种迭代方式方法引用的使用方法:
public class ForeachTest {public static void main(String[] args) {List<String> testList = new ArrayList<>();testList.add("a");testList.add("b");testList.add("c");//testList.forEach(new Consumer<String>(){////@Override//public void accept(String t) {//System.out.println(t);//}//});//替换成://testList.forEach( t -> System.out.println(t));//方法引用testList.forEach(System.out::println);}}
2 0
- java8函数式编程之三:集合中新增的方法:forEach
- Java8 编程规范入门之【forEach方法遍历集合】
- Java8 编程规范入门之【forEach方法遍历集合】
- Java8 编程规范入门之【forEach方法遍历集合】
- Java8中新增加的集合类
- Java8中新增加的集合类
- Java8中新增加的集合类
- Java8函数式编程之三:函数式接口
- java8之list集合中取出某一属性的方法
- Java8函数式编程之五:方法引用详解
- 使用Java8新增的Predicate操作集合
- 三中遍历数组和集合的方法 for foreach iterator
- Java8 Stream forEach()函数的效率问题
- Java8总结之函数式编程
- 【爱上Java8】Java8函数式方法的方法引用
- Java8 新特性之集合: forEach(Consumer<? super T> action)
- Java8 foreach方法
- JAVA8函数式编程
- 滴滴出行2017秋招笔试编程题(二)——地下迷宫、数字和为sum的方法数
- mysql 性能测试 参照
- System类-gc()
- Rxjava学习笔记
- 大数幂运算
- java8函数式编程之三:集合中新增的方法:forEach
- vue实现简易留言板(todolist)
- Android属性动画完全解析 (ValueAnimator,ObjectAnimator)(上)
- 【Unity&图片】图片自动批导入问题
- ubuntu14.04 没有侧边栏,没有右键,无法打开终端
- jenkins+gitlab钩子+shell脚本基于git的tag实现App增量更新
- Linux常用软件安装
- Construct Binary Tree from Preorder and Inorder Traversal
- 怎么安装MyEclipse