
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java streams counting() method with examples
Count the number of elements in the stream using the Java streams counting() method. Following is an example to implement the Java Streams counting() method −
Example
import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; public class Demo { public static void main(String[] args) { Stream<String> stream = Stream.of("Kevin", "Jofra","Tom", "Chris", "Liam"); // count long count = stream.collect(Collectors.counting()); System.out.println("Number of elements in the stream = "+count); } }
Output
Number of elements in the stream = 5
Let us see another example wherein we have stream of integer elements −
Example
import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; public class Demo { public static void main(String[] args) { Stream<Integer> stream = Stream.of(5, 10, 20, 40, 80, 160); // count long count = stream.collect(Collectors.counting()); System.out.println("Number of elements in the stream = "+count); } }
Output
Number of elements in the stream = 6
- Related Articles
- Java signum() method with Examples
- Java lang.Long.toBinaryString() method with Examples
- Java cbrt() method with Examples
- Java ceil() method with Examples
- Java sqrt() method with Examples
- Java floor() method with Examples
- Java toDegrees() method with Examples
- Java Signature getAlgorithm() method with Examples
- Java Signature getInstance() method with Examples
- Java Signature getProvider() method with Examples
- Java Signature initSign() method with Examples
- Java Signature toString() method with Examples
- Java lang Integer.toHexString() Method with Examples
- Java lang Long.toOctalString() Method with Examples
- Collectors counting() method in Java 8

Advertisements