How to get Stream count in Java 8 - onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorial, we will see how to get Stream count in Java 8 using Stream count and Collector counting methods.
We can find the number of elements in a stream using two different methods. count() method from Stream class and Collectors.counting() static method from Collectors class.
Syntax:
long 	count()
Returns the count of elements in the stream. If no elements are present, the result is 0.
public static <T> Collector<T,?,Long> counting()
Returns a Collector accepting elements of type T that counts the number of input elements. If no elements are present, the result is 0.
Getting the Stream count using Stream’s count() method.
import java.util.stream.Stream;

public class StreamCount {
    public static void main(String[] args) {
        Stream<Integer> stream = Stream.of(1,2,3,4,5,6,7,8,9);
        System.out.println("Stream Count is: "+stream.count());
    }
}

Output:
Stream Count is: 9
We can also get the count of a stream on applying the filter condition.
import java.util.stream.Stream;

public class StreamCount {
    public static void main(String[] args) {
        Stream<Integer> stream = Stream.of(1,2,3,4,5,6,7,8,9);
        Long count = stream.filter(num->(num%2)==0).count();
        System.out.println("There are "+count+" even numbers in the Stream");
    }
}
Output:
There are 4 even numbers in the Stream
Collectors counting is a static method used to calculate the elements in a stream. If there are no elements in a Stream, it returns merely ‘0’ as a result.
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamCount {
    public static void main(String[] args) {
        Stream<Integer> stream = Stream.of(1,2,3,4,5,6);
        Long count = stream.collect(Collectors.counting());
        System.out.println("Stream Count: "+count);
    }
}
Output:
Stream Count: 6
Done!
Happy Learning 🙂

How to Filter a Map in Java 8
How to Merge Streams in Java 8
How to convert List to Map in Java 8
Java 8 Read File Line By Line Example
Java 8 How to get common elements from two lists
Java 8 Getting Min and Max values from a Stream
How to Convert Iterable to Stream Java 8
How Java 8 Stream generate random String and Numbers
Java 8 How to convert Stream to List
How to Convert Java 8 Stream to Array
Java 8 Stream API and Parallelism
Java 8 Stream Filter Example with Objects
Java8 Concatenate Arrays Example using Stream
count number of lines characters and words in a file Java
Java Program to Find the characters count in String

How to Filter a Map in Java 8
How to Merge Streams in Java 8
How to convert List to Map in Java 8
Java 8 Read File Line By Line Example
Java 8 How to get common elements from two lists
Java 8 Getting Min and Max values from a Stream
How to Convert Iterable to Stream Java 8
How Java 8 Stream generate random String and Numbers
Java 8 How to convert Stream to List
How to Convert Java 8 Stream to Array
Java 8 Stream API and Parallelism
Java 8 Stream Filter Example with Objects
Java8 Concatenate Arrays Example using Stream
count number of lines characters and words in a file Java
Java Program to Find the characters count in String
 


Δ
 Java8 – Install Windows
 Java8 – foreach
 Java8 – forEach with index
 Java8 – Stream Filter Objects
 Java8 – Comparator Userdefined
 Java8 – GroupingBy
 Java8 – SummingInt
 Java8 – walk ReadFiles
 Java8 – JAVA_HOME on Windows
 Howto – Install Java on Mac OS
 Howto – Convert Iterable to Stream
 Howto – Get common elements from two Lists
 Howto – Convert List to String
 Howto – Concatenate Arrays using Stream
 Howto – Remove duplicates from List
 Howto – Filter null values from Stream
 Howto – Convert List to Map
 Howto – Convert Stream to List
 Howto – Sort a Map
 Howto – Filter a Map
 Howto – Get Current UTC Time
 Howto – Verify an Array contains a specific value
 Howto – Convert ArrayList to Array
 Howto – Read File Line By Line
 Howto – Convert Date to LocalDate
 Howto – Merge Streams
 Howto – Resolve NullPointerException in toMap
 Howto -Get Stream count
 Howto – Get Min and Max values in a Stream
 Howto – Convert InputStream to String