long count ) )返回流中元素的数量。 这是归纳性的特殊情况。 归纳运算采用一系列输入要素,通过反复应用组合运算归纳为一个总结结果。 这是终端操作,可能会遍历流以产生结果和副作用。 执行终端操作后,流程线将被视为已消耗,且不可重用。
使用方法:
long count () )
注意:计数操作的返回值是流中元素的计数。
示例1:计算数组中的元素数。
//Java code for Stream.count (
//tocounttheelementsinthestream。
import java.util.*;
类gfg {
//驱动器代码
publicstaticvoidmain (string [ ] args ) )。
{
//creating a list of Integers
list list=arrays.as list (0,2,4,6,
八、十、十二;
//Using count () to count the number
//of elements in the stream and
//storing the result in a variable。
long total=list.stream ().count );
//displayingthenumberofelements
system.out.println(total );
}
}
输出:
7
示例2:计算列表中不同元素的数量。
//Java code for Stream.count (
/