LongStream count() returns the count of elements in the stream. LongStream count() is present in java.util.stream.LongStream
Syntax :
long count()
Example 1 : Count the elements in LongStream.
import java.util.*;
import java.util.stream.LongStream;
class GFG {
public static void main(String[] args)
{
LongStream stream = LongStream.of(2L, 3L, 4L, 5L,
6L, 7L, 8L);
long total = stream.count();
System.out.println(total);
}
}
|
Example 2 : Count the elements in a given range.
import java.util.*;
import java.util.stream.LongStream;
class GFG {
public static void main(String[] args)
{
LongStream stream = LongStream.range( 2 , 50 );
long total = stream.count();
System.out.println(total);
}
}
|
Example 3 : Count distinct elements in LongStream.
import java.util.*;
import java.util.stream.LongStream;
class GFG {
public static void main(String[] args)
{
LongStream stream = LongStream.of(2L, 3L, 4L, 4L,
7L, 7L, 8L);
long total = stream.distinct().count();
System.out.println(total);
}
}
|