Stream count() method in Java with examples Last Updated : 06 Dec, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 traverse the stream to produce a result or a side-effect. After the terminal operation is performed, the stream pipeline is considered consumed, and can no longer be used. Syntax : long count() Note : The return value of count operation is the count of elements in the stream. Example 1 : Counting number of elements in array. Java // Java code for Stream.count() // to count the elements in the stream. import java.util.*; class GFG { // Driver code public static void main(String[] args) { // creating a list of Integers List<Integer> list = Arrays.asList(0, 2, 4, 6, 8, 10, 12); // Using count() to count the number // of elements in the stream and // storing the result in a variable. long total = list.stream().count(); // Displaying the number of elements System.out.println(total); } } Output : 7 Example 2 : Count number of distinct elements in a list. Java // Java code for Stream.count() // to count the number of distinct // elements in the stream. import java.util.*; class GFG { // Driver code public static void main(String[] args) { // creating a list of Strings List<String> list = Arrays.asList("GFG", "Geeks", "for", "Geeks", "GeeksforGeeks", "GFG"); // Using count() to count the number // of distinct elements in the stream and // storing the result in a variable. long total = list.stream().distinct().count(); // Displaying the number of elements System.out.println(total); } } Output : 4 Comment More infoAdvertise with us Next Article Stream peek() Method in Java with Examples S Sahil_Bansall Follow Improve Article Tags : Misc Java Java - util package Java-Functions java-stream Java-Stream interface +2 More Practice Tags : JavaMisc Similar Reads 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 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 BitSet stream() Method in Java with Examples The stream() method of Java BitSet class is used to return a stream of indices for every bit contained in the BitSet. The indices are returned in increasing order. The size of the stream is the number of bits in the set state of the BitSet, which is equal to the value returned by the cardinality() m 2 min read Stream peek() Method in Java with Examples In Java, Stream provides an powerful alternative to process data where here we will be discussing one of the very frequently used methods named peek() which being a consumer action basically returns a stream consisting of the elements of this stream, additionally performing the provided action on ea 2 min read Stream skip() method in Java with examples Prerequisite : Streams in java The skip(long N) is a method of java.util.stream.Stream object. This method takes one long (N) as an argument and returns a stream after removing first N elements. skip() can be quite expensive on ordered parallel pipelines, if the value of N is large, because skip(N) 3 min read Stream dropWhile() method in Java with examples The dropWhile(java.util.function.Predicate) method returns two different types of stream depending upon whether the stream is ordered or not. If the stream is ordered then a stream of the remaining elements of this stream after dropping the longest prefix of elements that match the given predicate i 3 min read Like