DoubleStream count()返回流中元素的數量。 DoubleStream count()存在於java.util.stream.DoubleStream中。這是歸約的一種特殊情況,即,它采用一係列輸入元素並將它們組合為一個匯總結果。
用法:
long count()
注意:這是終端操作,即可能會遍曆流以產生結果或副作用。執行終端操作後,流管線被視為已消耗,無法再使用。
示例1:計算DoubleStream中的元素。
// Java code for DoubleStream count()
// to count the number of elements in
// given stream
import java.util.*;
import java.util.stream.DoubleStream;
class GFG {
// Driver code
public static void main(String[] args)
{
// creating a DoubleStream
DoubleStream stream = DoubleStream.of(2.3, 3.4, 4.5,
5.6, 6.7, 7.8, 8.9);
// 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:計算DoubleStream中的不同元素。
// Java code for DoubleStream count()
// to count the number of distinct
// elements in given stream
import java.util.*;
import java.util.stream.DoubleStream;
class GFG {
// Driver code
public static void main(String[] args)
{
// creating a DoubleStream
DoubleStream stream = DoubleStream.of(2.2, 3.3, 4.4,
4.4, 7.6, 7.6, 8.0);
// 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 DoubleStream max()用法及代碼示例
- Java DoubleStream min()用法及代碼示例
- Java DoubleStream peek()用法及代碼示例
- Java DoubleStream allMatch()用法及代碼示例
- Java DoubleStream anyMatch()用法及代碼示例
- Java DoubleStream noneMatch()用法及代碼示例
- Java DoubleStream limit()用法及代碼示例
- Java DoubleStream builder()用法及代碼示例
- Java DoubleStream average()用法及代碼示例
- Java DoubleStream empty()用法及代碼示例
- Java DoubleStream filter()用法及代碼示例
- Java DoubleStream boxed()用法及代碼示例
- Java DoubleStream distinct()用法及代碼示例
- Java DoubleStream.Builder add(double t)用法及代碼示例
- Java DoubleStream reduce(double identity, DoubleBinaryOperator op)用法及代碼示例
注:本文由純淨天空篩選整理自Sahil_Bansall大神的英文原創作品 DoubleStream count() in Java with examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。