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