Java streams counting() method with examples Last Updated : 06 Dec, 2018 Comments Improve Suggest changes Like Article Like Report 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’ value of type Long. It is a terminal operation which returns the total count of elements in the stream which reach the collect() method after undergoing various pipelined stream operations such as filtering. Example 1 : To count the elements in stream of Integers. Java // Java code to count number of elements // in stream import java.util.stream.Stream; import java.util.stream.Collectors; class counting { public static void main(String[] args) { // creating stream of integers Stream<Integer> i = Stream.of(1, 2, 3, 4, 5, 6); // counting number of integer in stream long count_int = i.collect(Collectors.counting()); System.out.println(count_int); } } Output: 6 Example 2 : To count the elements in stream of String. Java // JAVA code to count number of elements in stream import java.util.stream.Stream; import java.util.stream.Collectors; class counting { public static void main(String[] args) { // creating stream of strings Stream<String> s = Stream.of("Akash","Harsh", "Shubham","Nishant","Pratik"); // counting number of strings in stream long count_string = s.collect(Collectors.counting()); System.out.println(count_string); } } Output: 5 Comment A akash1295 Follow 0 Improve A akash1295 Follow 0 Improve Article Tags : Java Java - util package Java-Functions java-stream Java-Stream interface +1 More Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings8 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java9 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface4 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like