Introduction

The addition of the Stream API was one of the major features added to Java 8. A Stream in Java can be defined as a sequence of elements from a source that supports aggregate operations on them. The source here refers to collections or arrays that provide data to a stream.

A few important points about streams are:

  1. A stream is not a data structure itself. It is a bunch of operations applied to a source. The source can be collections, arrays or I/O channels.

  2. Streams don’t change the original data structure.

  3. There can be zero or more intermediate operations that transform a stream into another stream.

  4. Each intermediate operation is lazily executed (This will be discussed later).

  5. Terminal operations produce the result of the stream.

Stream creation

Streams can be created from different element sources, e.g., a collection or an array with the help of stream() and of() methods. Below are the different ways to create a stream.

a) Stream.of(v1, v2, v3….)

In the below example, we are creating a stream of integers at line 7 using the Stream.of() method.

Get hands-on with 1200+ tech skills courses.