s-day07- Stream流中的常用方法_forEach
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
package com.inmind.stream07;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/*
|
||||
Stream流中的常用方法_forEach
|
||||
void forEach(Consumer<? super T> action) 对此流的每个元素执行操作。
|
||||
|
||||
知识点回顾:
|
||||
Consumer:接收一个参数,没有返回值
|
||||
抽象方法:void accept(T t) ---> t->{}
|
||||
|
||||
foreach的执行原理:它会将Stream流中的每个元素,作为以上Consumer的参数执行方法体
|
||||
foreach的作用:是对流中的元素进行遍历消费
|
||||
*/
|
||||
public class ForeachDemo14 {
|
||||
public static void main(String[] args) {
|
||||
Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);
|
||||
System.out.println(stream);
|
||||
stream.forEach(i-> System.out.println(i));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user