IntStream count()返回流中元素的数量。 java.util.stream.IntStream中存在IntStream count()
用法:
long count()
示例1:计算IntStream中的元素。
// Java code for IntStream count()
// to count the number of elements in
// given stream
import java.util.*;
import java.util.stream.IntStream;
class GFG {
// Driver code
public static void main(String[] args)
{
// creating a stream
IntStream stream = IntStream.of(2, 3, 4, 5, 6, 7, 8);
// storing the count of elements
// in a variable named total
long total = stream.count();
// displaying the total number of elements
System.out.println(total);
}
}
输出:
7
示例2:计算给定范围内的元素。
// Java code for IntStream count()
// to count the number of elements in
// given range excluding the last element
import java.util.*;
import java.util.stream.IntStream;
class GFG {
// Driver code
public static void main(String[] args)
{
// creating a stream
IntStream stream = IntStream.range(2, 50);
// storing the count of elements
// in a variable named total
long total = stream.count();
// displaying the total number of elements
System.out.println(total);
}
}
输出:
48
示例3:计算IntStream中的不同元素。
// Java code for IntStream count()
// to count the number of distinct
// elements in given stream
import java.util.*;
import java.util.stream.IntStream;
class GFG {
// Driver code
public static void main(String[] args)
{
// creating a stream
IntStream stream = IntStream.of(2, 3, 4, 4, 7, 7, 8);
// storing the count of distinct elements
// in a variable named total
long total = stream.distinct().count();
// displaying the total number of elements
System.out.println(total);
}
}
输出:
5
相关用法
- Java IntStream max()用法及代码示例
- Java IntStream min()用法及代码示例
- Java IntStream distinct()用法及代码示例
- Java IntStream peek()用法及代码示例
- Java IntStream empty()用法及代码示例
- Java IntStream average()用法及代码示例
- Java IntStream filter()用法及代码示例
- Java IntStream allMatch()用法及代码示例
- Java IntStream anyMatch()用法及代码示例
- Java IntStream toArray()用法及代码示例
- Java IntStream noneMatch()用法及代码示例
- Java IntStream reduce(IntBinaryOperator op)用法及代码示例
- Java IntStream codePoints()用法及代码示例
- Java IntStream.Builder build()用法及代码示例
- Java IntStream reduce(int identity, IntBinaryOperator op)用法及代码示例
注:本文由纯净天空筛选整理自Sahil_Bansall大神的英文原创作品 IntStream count() in Java with examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。