Java Basic Tutorial
Java Advance Tutorial
The Stream API in Java is used for functional-style operations on collections of data. It was introduced in Java 8 and provides a powerful way to process sequences of elements.
Key Features of Stream API
Declarative – Focuses on what to do, not how to do it.
Lazy Evaluation – Operations are performed only when necessary.
Functional Programming – Uses lambda expressions and method references.
Parallel Processing – Improves performance on large data sets.
Non-Mutable – Does not modify the original collection.
There are two types of operations in Stream API:
filter()
, map()
, sorted()
).forEach()
, collect()
, reduce()
).
Method |
Description |
---|---|
Filters elements based on a condition. |
|
Collects results into a list. |
|
Transforms elements. |
|
Sorts elements. |
|
Counts elements in the stream. |
|
Combines all elements into one. |
|
Checks conditions on elements. |
filter()
) and Collecting Elements (collect()
) :map()
):Modifies each element in the stream.
sorted()
):Sorts elements in natural order or custom order.
count()
):Counts the number of elements in the stream.
reduce()
):Combines all elements into a single value.
anyMatch()
, allMatch()
):Java provides parallel streams for multi-threaded processing.