
- 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
Stream In Java
Stream represents a sequence of objects from a source, which supports aggregate operations. Following are the characteristics of a Stream −
Sequence of elements − A stream provides a set of elements of specific type in a sequential manner. A stream gets/computes element on demand. It never stores the elements.
Source − Stream takes Collections, Arrays, or I/O resources as input source.
Aggregate operations − Stream supports aggregate operations like filter, map, limit, reduce, find, match, and so on.
Pipelining − Most of the stream operations return stream itself so that their result can be pipelined. These operations are called intermediate operations and their function is to take input, process them, and return output to the target. collect() method is a terminal operation which is normally present at the end of the pipelining operation to mark the end of the stream.
Automatic iterations − Stream operations do the iterations internally over the source elements provided, in contrast to Collections where explicit iteration is required.
Example
Let us now see an example −
import java.util.Collection; import java.util.TreeSet; 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", "10", "15", "20", "25"); Collection<String> collection = stream.collect(Collectors.toCollection(TreeSet::new)); System.out.println("Collection = "+collection); } }
Output
Collection = [100, 130, 150, 20, 200, 50, 80]
Example
Now, let us count the number of elements in the stream using the Java streams counting() method −
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
- Related Articles
- Character Stream vs Byte Stream in Java\n
- Stream sorted() in Java
- Java Stream Collectors toCollection() in Java
- Array To Stream in Java
- Difference between the byte stream and character stream classes in Java?
- Convert Stream to Set in Java
- Convert an Iterator to Stream in Java
- Convert an Iterable to Stream in Java
- Java 8 Stream Terminal Operations
- Java Stream findAny() with examples
- Conversion of Set To Stream in Java\n
- Conversion of Stream To Set in Java\n
- How to convert Stream to TreeSet in Java?
- Program to convert List to Stream in Java
- Sum of list with stream filter in Java
