进阶day07-Stream流中的常用方法_forEach(流的遍历)
This commit is contained in:
23
javaSE-day07/src/com/inmind/stream03/ForEachDemo18.java
Normal file
23
javaSE-day07/src/com/inmind/stream03/ForEachDemo18.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package com.inmind.stream03;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/*
|
||||
学习内容:Stream流中的常用方法_forEach
|
||||
void forEach(Consumer action) 对此流的每个元素执行消费操作。
|
||||
|
||||
知识点回顾: Consumer:接收一个参数用来操作,并没有返回值
|
||||
抽象方法:void accept(T t) --->对应的lambda对应的格式:t->{}
|
||||
foreach的执行原理:它会将stream流 中的每个元素作为以上lambda的参数执行方法体.
|
||||
*/
|
||||
//需求:将整数进行相加10,再打印操作
|
||||
public class ForEachDemo18 {
|
||||
public static void main(String[] args) {
|
||||
//获取流
|
||||
Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);
|
||||
//遍历操作
|
||||
stream.forEach((Integer i)->{
|
||||
System.out.println(i+10);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user