

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Collectors counting() method in Java 8
The counting() method of the Java 8 Collectors class returns a Collector accepting elements of type T that counts the number of input elements.
The syntax is as follows −
static <T> Collector<T,?,Long> counting()
Here, the parameter −
T − type of input elements
Long − This class value of the primitive type long in an object
To work with Collectors class in Java, import the following package −
import java.util.stream.Collectors;
The following is an example to implement counting() method in Java 8 −
Example
import java.util.stream.Collectors; import java.util.stream.Stream; public class Demo { public static void main(String[] args) { Stream<String> stream = Stream.of("25", "50", "75", "100", "125", "150", "200"); long res = stream.collect(Collectors.counting()); System.out.println("Count of elements in the stream = "+res); } }
Output
Count of elements in the stream = 7
- Related Questions & Answers
- Collectors toSet() method in Java 8
- Collectors averagingLong () method in Java 8
- Collectors averagingDouble() method in Java 8
- Collectors toList() method in Java 8
- Collectors averagingInt() method in Java 8
- Collectors toCollection() method in Java 8
- Collectors maxBy() method in Java 8
- Collectors minBy() method in Java 8
- Collectors collectingAndThen() method in Java 8
- Collectors partitioningBy() method in Java 8
- Collectors.joining() method in Java 8
- Java streams counting() method with examples
- Java 8 Clock equals() Method
- Java 8 Clock millis() Method
- Java 8 Clock instant() method
Advertisements