0% found this document useful (0 votes)
6 views10 pages

Stream API in Java 8

The Stream API in Java 8 allows for processing collections of data in a functional style, utilizing operations like map(), filter(), collect(), and reduce(). It emphasizes a pipeline approach with intermediate and terminal operations for data transformation and aggregation. Streams enhance code clarity and efficiency, making it easier to handle data processing tasks.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views10 pages

Stream API in Java 8

The Stream API in Java 8 allows for processing collections of data in a functional style, utilizing operations like map(), filter(), collect(), and reduce(). It emphasizes a pipeline approach with intermediate and terminal operations for data transformation and aggregation. Streams enhance code clarity and efficiency, making it easier to handle data processing tasks.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Stream API in Java 8 —

map(), filter(),
collect(), reduce()
Explained 🌊
“Process data with elegance and power.”
What is Stream API?

Stream API helps you process


collections of data in a declarative,
functional style.

Before Java 8 : imperative loops


After Java 8 : functional pipelines ✅
🧠 Stream = sequence of elements
supporting operations to transform
data.
Stream Workflow
A Stream pipeline =
1️⃣ Source →(Collection, Array, I/O)
2️⃣ Intermediate Operations →
(filter,
map, sorted, distinct)
3️⃣ Terminal Operations →
(collect,
count, forEach, reduce)

Example :
[Link]()
.filter(x -> x > 10)
.map(x -> x * 2)
.collect([Link]());
map() → Transformation 🔄
Used to transform data elements into
another form.

Example :

List<String> names = [Link]("java", "python", "c++");

List<String> upper = [Link]()


.map(String::toUpperCase)
.collect([Link]());

🧠 Output : [JAVA, PYTHON, C++]


map() = one-to-one transformation.

filter() Conditional
Selection 🔍
Used to filter elements based on a
condition.

Example:
List<Integer> nums = [Link](1, 2, 3, 4, 5, 6);

[Link]()
.filter(n -> n % 2 == 0)
.forEach([Link]::println);

🧠 Output : 2 4 6
Keeps only the elements that satisfy the
condition.

collect() Terminal
Operation 📦
Collects the result of Stream
operations into a collection or other
structure.

Example :

List<String> result = [Link]()


.filter(n -> [Link]("P"))
.collect([Link]());

Collectors = Utility class with static


methods like
toList(), toSet(), joining(), groupingBy().

reduce() Aggregation /
Reduction 🧮
Used to combine all stream elements
into a single result (sum, min, max,
etc.)

Example :

int sum = [Link]()


.reduce(0, (a, b) -> a + b);

🧠 Output : Sum = 21
reduce() = takes a starting value +
accumulator function.
Stream Chaining
Example ⚙️
List<Integer> squares = [Link]()
.filter(n -> n % 2 == 0)
.map(n -> n * n)
.collect([Link]());

Input : [1, 2, 3, 4, 5, 6]

Output : [4, 16, 36]
Filter → Map → Collect — the essence of
Stream pipelines.
Common Interview Questions 💬
Q1: Difference between map() and flatMap()?
→ map() transforms each element,
→ flatMap() flattens nested structures.

Q2: Are Streams reusable?


❌ No, a Stream can be consumed only once.

Q3: Difference between Intermediate &


Terminal ops?
→ Intermediate returns Stream, Terminal
returns result.

Q4: How are Streams different from


Collections?
→ Collections store data; Streams process
data.

Q5: Are Streams lazy?


✅ Yes. Operations execute only when a
terminal op is invoked.
Summary

✔ Stream = data processing pipeline


✔ map() → transform
✔ filter() → select
✔ collect() → aggregate
✔ reduce() → summarize
💡 Streams make your Java code
cleaner, faster, and parallel-ready.

Follow for More

You might also like