Common methods in Stream stream _forEach
void forEach(Consumer<? super T> action);
This method receives a Consumer interface function, and will hand each stream element to the function for processing.
The Consumer interface is a consumer interface that can pass Lambda expressions and consume data
简单记:
forEach 方法,用来遍历流中的其他方法
是一个终结方法,遍历之后就不能继续调用Stream流中的其他方法
Example:
public class Demo02Stream_forEach {
public static void main(String[] args) {
//获取一个Stream流
Stream<String> stream = Stream.of("张三", "李四", "王五", "赵柳");
//使用Stream流中的方法forEach对Stream流中的数据进行遍历
stream.forEach(name-> System.out.println(name));
}
}
Program demonstration: