s-day07- Stream流中的常用方法_count
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
package com.inmind.stream07;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/*
|
||||
Stream流中的常用方法_count
|
||||
long count() 返回此流中的元素数
|
||||
流是一个流水生产线,是不能倒退的。count()是一个终结方法,就是这个流结束,那个该流就无法再操作
|
||||
*/
|
||||
public class CountDemo16 {
|
||||
public static void main(String[] args) {
|
||||
Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);
|
||||
long count = stream.count();
|
||||
System.out.println(count);
|
||||
//java.lang.IllegalStateException: stream has already been operated upon or closed
|
||||
// stream.forEach(i -> System.out.println(i));//错误,因为流已经结束,不能再操作了
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user