IntStream count() in Java with examples
Last Updated :
06 Dec, 2018
IntStream count() returns the count of elements in the stream. IntStream count() is present in java.util.stream.IntStream
Syntax :
long count()
Example 1 : Count the elements in IntStream.
import java.util.*;
import java.util.stream.IntStream;
class GFG {
public static void main(String[] args)
{
IntStream stream = IntStream.of( 2 , 3 , 4 , 5 , 6 , 7 , 8 );
long total = stream.count();
System.out.println(total);
}
}
|
Output :
7
Example 2 : Count the elements in a given range.
import java.util.*;
import java.util.stream.IntStream;
class GFG {
public static void main(String[] args)
{
IntStream stream = IntStream.range( 2 , 50 );
long total = stream.count();
System.out.println(total);
}
}
|
Output :
48
Example 3 : Count distinct elements in IntStream.
import java.util.*;
import java.util.stream.IntStream;
class GFG {
public static void main(String[] args)
{
IntStream stream = IntStream.of( 2 , 3 , 4 , 4 , 7 , 7 , 8 );
long total = stream.distinct().count();
System.out.println(total);
}
}
|
Output :
5
Share your thoughts in the comments
Please Login to comment...