Explain Stream API introduced in Java 8 ?

By | September 8, 2019

In this blog post we will talk and learn about one of very important java 8 interview question and question is that Explain Stream API introduced in Java 8 ?

What is a Stream ?

  • A stream can be define as a sequence of data elements supporting sequential and parallel aggregate operations.
  • Example like computing the sum of all elements in a stream of integers, mapping all names in list to their lengths, aggregate operations on streams etc..

How is it different than Collection ?

Collections focus on the storage of data elements for efficient access whereas streams focus on aggregate computations on data elements from a data source.

Few Important key points about Stream are –

  • Streams have no storage.
  • Streams can represent a sequence of infinite elements.
  • The design of streams is based on internal iteration.
  • Streams are designed to be processed in parallel with no additional work from the developers.
  • Streams are designed to support functional programming.
  • Streams support lazy operations.
  • Streams can be ordered or unordered.
  • Streams cannot be reused.

What is ParallelStream ?

Java provides two main functionalities out of the box –

a)Partitioning of the stream of data into smaller streams to make parallel processing possible.
b)Parallel processing of the data

Can you provide some example of Stream API?

Creating Stream from Values

Output:

Stream of Strings……
John
Sean
Sam
Bingel
Stream of Integers……
10
20
30
40
50
Stream of finite Integers……
1
2
3
4
5
6
7
8
9
10

Stream Functions example

Output:

Getting first five odd numbers using Stream..
1
3
5
7
9
Getting first five random numbers using Stream..
0.8260326471627876
0.9341251235732477
0.8863277196070732
0.7915521185380437
0.34936404210516936

Creating Stream from Arrays and Collections

Output:

Getting Stream from array..
100
200
300
400
500
600
Getting Square all the numbers in the array using Stream..
1
4
9
16
25
Create a sequential stream from the set..
Sean
Marry
Create a parallel stream from the set
Sean
Marry
Reading file content using Stream…
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
This is line 6
This is line 7
This is line 8
This is line 9
This is line 10

Operations on Streams (Bulk Operations, Map and Reduce)

Output:

Taking integer: 1
Filtered integer: 1
Mapped integer: 1
Taking integer: 2
Taking integer: 3
Filtered integer: 3
Mapped integer: 9
Taking integer: 4
Taking integer: 5
Filtered integer: 5
Mapped integer: 25
Taking integer: 7
Filtered integer: 7
Mapped integer: 49
sum = 84

Second Example of Operations on Streams (Bulk Operations, Map and Reduce)

Employee.java class

Content of test.txt file

Client program:

Output:

Get All males having income greater than 10000
Sam
Raj
Get Combined Income of All Employees
83000.0
Increase Income of All Females by 10%
Employee [id=1002, name=Sean, isFemale=false, isMale=true, salary=10000.0]
Employee [id=1072, name=Marry, isFemale=true, isMale=false, salary=16500.0]
Employee [id=1882, name=Sam, isFemale=false, isMale=true, salary=30000.0]
Employee [id=1992, name=Sofiya, isFemale=true, isMale=false, salary=3300.0000000000005]
Employee [id=1112, name=Raj, isFemale=false, isMale=true, salary=16000.0]
Employee [id=1772, name=Hema, isFemale=true, isMale=false, salary=9900.0]

That’s all about  Explain Stream API introduced in Java 8 ?

You May Also Like:

What are new Features added in Java 8?
How to Convert List to Map using Java 8
Java Spliterator Example
Differences Between map() And flatMap() in Java 8 ?
How will you find Word Frequency in sorted order for a collection of words

If you have any feedback or suggestion please feel free to drop in blow comment box. 

Leave a Reply

Your email address will not be published. Required fields are marked *