Javatpoint Logo
Javatpoint Logo

Two Types of Streams Offered by Java

In Java, the streams are majorly utilized for providing and offering several programming paradigms that are used for data processing and in an expensive and concise manner.

Java contains two major stream types and they are Intermediate Streams and Terminal Streams.

Let's understand about both Intermediate Stream and Terminal Stream in detail in the following paragraphs.

Intermediate Streams

Intermediate stream operations can be defined as operations that are utilized for transforming one stream into another stream. The operations of intermediate stream are often chained together for the creation of a transformation pipeline. Also, the operations of intermediate are lazy which means that they do not complete execution until and unless an operation of terminal is called. This evaluation in lazy manner results in higher efficient processing when data is being dealt in large datasets.

Let's understand about Intermediate streams in Java with the help of a Java example program.

Example 1:

Filename: IntermediateStreamsExample.java

Output:

People aged 30 or older: [Person{name='Bob', age=35}, Person{name='Diana', age=30}]
Names in uppercase: [ALICE, BOB, CHARLIE, DIANA, EVA]
People sorted by age (descending): [Person{name='Bob', age=35}, Person{name='Diana', age=30}, Person{name='Alice', age=28}, Person{name='Eva', age=25}, Person{name='Charlie', age=22}]
Distinct ages: [28, 35, 22, 30, 25]

Terminal Streams in Java

In Java, the operations of Terminal streams can be defined as operations that are utilized in producing results or side effects. When a single operation of terminal is activated or invoked, the stream gets consumed, and no more operations will be allowed or permitted to be applied to it. The terminal operations are also utilized in triggering the evaluation of the whole pipeline of the stream.

Let's understand about Intermediate streams in Java with the help of a Java example program.

Filename: TerminalStreamsExample.java

Output:

Printing each person's details:
Person{name='Alice', age=28}
Person{name='Bob', age=35}
Person{name='Charlie', age=22}
Person{name='Diana', age=30}
Person{name='Eva', age=25}
List of names: [Alice, Bob, Charlie, Diana, Eva]
Sum of ages: 140
Number of people: 5
Anyone older than 35? false
All younger than 40? true
None older than 40? true

This was all the required information and explanation about two types of streams offered by Java.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA