0% found this document useful (0 votes)
22 views

Java Streams

Uploaded by

azeemmubarak0211
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Java Streams

Uploaded by

azeemmubarak0211
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Java

Streams
Introduction to Java Streams
Java Streams are a powerful tool introduced in Java
8 as part of the Java Stream API.

They allow developers to process sequences of


elements in a functional style.

Streams bring the power of functional programming


to Java, enabling operations on collections and
arrays in a more concise, readable, and efficient way.

Streams are wrappers around a data source, allowing


us to operate with that data source and making bulk
processing convenient and fast.

Java 8 Streams should not be confused with Java I/O


stream, these have very little to do with each other.
Core Concepts of Java Streams
1. Stream Definition:
A Stream is a sequence of elements
supporting sequential and parallel aggregate
operations.
2. Lazy Evaluation:
Streams are lazy; computation on the source
data is only performed when the terminal
operation is initiated, allowing optimization.
3. Intermediate vs. Terminal Operations:
Intermediate Operations: Return a stream and
are lazy (e.g., filter, map).
Terminal Operations: Produce a result or side-
effect and terminate the stream (e.g., forEach,
reduce, collect).
4. Statelessness and Immutability:
Stream operations do not modify the source;
instead, they return a new stream with the
result.
Creating Streams
1. From Collection

2. From Arrays

3. From Values

4. Infinite Streams
Intermediate Operations
1. Filter
Filters elements based on a predicate.

2. map
Transforms each element using a given function.

3. sorted
Sorts the stream elements.
Intermediate Operations
4. distinct
Removes duplicate elements

5. limit and skip


Limits the number of elements and skips the first N
elements.
Terminal Operations
1. forEach
Performs an action for each element.

2. collect
Collects the stream elements into a collection.

3. reduce
Reduces the stream to a single value using a binary
opeartor.
Terminal Operations
4. toArray
Converts the stream into an array.

5. findFirst and findAny


Finds the first or any element in the stream.

6. count
Counts the number of elements.
Terminal Operations
7. allMatch , anyMatch , noneMatch
Checks if all, any, or none of the elements match a
predicate.
Benefits of Using Streams
1. Concise and Readable Code:
Stream operations reduce boilerplate code and
improve readability.
2. Parallel Processing:
Streams can easily be parallelized to leverage
multi-core architectures using parallelStream.
3. Improved Performance:
Lazy evaluation and optimization opportunities can
lead to performance gains.
4. Functional Programming Paradigm:
Streams enable a more functional approach to
problem-solving, enhancing code maintainability.
Example
Thank You

You might also like