IntStream count() in Java with examples Last Updated : 06 Dec, 2018 Comments Improve Suggest changes Like Article Like Report 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. Java // 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); } } Output : 7 Example 2 : Count the elements in a given range. Java // 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); } } Output : 48 Example 3 : Count distinct elements in IntStream. Java // 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); } } Output : 5 Comment More infoAdvertise with us Next Article Stream count() method in Java with examples S Sahil_Bansall Follow Improve Article Tags : Misc Java Java - util package Java-Functions java-stream java-intstream +2 More Practice Tags : JavaMisc Similar Reads IntStream peek() in Java with examples IntStream peek() is a method in java.util.stream.IntStream. The function returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream. Syntax : IntStream peek(IntConsumer action) Where, IntS 3 min read LongStream count() in Java with examples 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. Java // Java code for LongStream count() // to count the number of elements in // given stream import java. 2 min read Stream count() method in Java with examples long count() returns the count of elements in the stream. This is a special case of a reduction (A reduction operation takes a sequence of input elements and combines them into a single summary result by repeated application of a combining operation). This is a terminal operation i.e, it may travers 2 min read Java streams counting() method with examples In Java 8, there is predefined counting() method returned by Collectors class counting() method to count the number of elements in a Stream. Syntax : public static Collector counting() Where, output is a Collector, acting on a Stream of elements of type T, with its finisher returning the âcollectedâ 2 min read Java lang.Long.builtcount() method in Java with Examples java.lang.Long.bitCount() is a built in function in Java that returns the number of set bits in a binary representation of a number. It accepts a single mandatory parameter number whose number of set bits is returned. Syntax: public static long bitCount(long num) Parameters: num - the number passed 3 min read Java.util.IntSummaryStatistics class with Examples The IntSummaryStatistics class is present in java.util package. It takes a collection of Integer objects and is useful in the circumstances when we are dealing with a stream of integers. It maintains a count of the number of integers it has processed, their sum and various other statistics. The clas 3 min read Like