Streams
About
What Are Streams?
Example: Traditional Loop vs Stream Processing
Without Streams (Imperative Approach)
List<String> names = List.of("Alice", "Bob", "Charlie", "David");
List<String> filteredNames = new ArrayList<>();
for (String name : names) {
if (name.startsWith("A")) {
filteredNames.add(name);
}
}
System.out.println(filteredNames); // [Alice]With Streams (Declarative Approach)
Why use Streams?
Key Characteristics of Streams
1. Streams Do Not Store Data
2. Streams Are Functional in Nature
3. Streams Are Lazy (Lazy Evaluation)
4. Streams Support Parallel Execution
5. Streams Support Pipeline Processing
6. Streams Have Two Types of Operations
Memory Usage
Overview
1️. Lazy Evaluation (Efficient Memory Usage)
Example: Lazy Execution (Efficient)
Example: Eager Execution (Memory-Intensive)
2️. Intermediate Operations and Memory Impact
3️. Parallel Streams and Memory Consumption
4️. Collecting Data (collect() & Memory Allocation)
collect() & Memory Allocation)5️. Large Data Streams (Handling Gigabytes of Data)
6. Memory Allocation When Creating Multiple Streams
Stream Operations Overview
1. What Are Intermediate and Terminal Operations?
Intermediate Operations (Lazy and Return a Stream)
Terminal Operations (Trigger Execution and Produce a Result)
2. Understanding the Stream Pipeline
1. Data Source
2. Intermediate Operations (Lazy)
3. Terminal Operation (Triggers Execution)
Complete Stream Pipeline Example
Pipeline Execution Order (Optimization)
Creating Streams in Java
1. Creating Streams from Collections
Parallel Stream from a Collection
2. Creating Streams from Arrays
Stream from a Primitive Array
3. Using Stream.of(), Stream.generate(), and Stream.iterate()
Stream.of(), Stream.generate(), and Stream.iterate()Stream.of() – Creating Streams from Values
Stream.of() – Creating Streams from ValuesStream.generate() – Infinite Stream with Supplier
Stream.generate() – Infinite Stream with SupplierStream.iterate() – Infinite Stream with Iteration
Stream.iterate() – Infinite Stream with IterationIntermediate Operations in Java Streams
1. Filtering with filter()
filter()2. Transforming with map()
map()3. Flattening with flatMap()
flatMap()4. Removing Duplicates with distinct()
distinct()5. Sorting Elements with sorted()
sorted()Custom Sorting
6. Debugging with peek()
peek()Terminal Operations in Java Streams
1. Iterating with forEach()
forEach()2. Collecting with collect()
collect()Grouping elements
3. Counting with count()
count()4. Finding Min/Max with min() and max()
min() and max()5. Reducing with reduce()
reduce()Sum of all elements
Concatenating Strings
6. Matching Elements with anyMatch(), allMatch(), noneMatch()
anyMatch(), allMatch(), noneMatch()anyMatch() – At least one element matches
anyMatch() – At least one element matchesallMatch() – All elements match
allMatch() – All elements matchnoneMatch() – No elements match
noneMatch() – No elements matchParallel Streams in Java
1. What are Parallel Streams?
How to Create a Parallel Stream?
2. When to Use Parallel Streams?
Example: Using Parallel Stream for Sum Calculation
3. Performance Considerations for Parallel Execution
When Parallel Streams are Beneficial:
When NOT to Use Parallel Streams:
Example: Incorrect Use of Parallel Streams (Race Condition)
Primitive Streams (IntStream, LongStream, DoubleStream)
1. Why Use Primitive Streams?
2. Creating and Processing Primitive Streams
2.1 Creating Primitive Streams
3. Specialized Operations for Numeric Streams
3.1 Summing Elements (sum())
sum())3.2 Finding Min and Max (min(), max())
min(), max())3.3 Finding the Average (average())
average())3.4 Collecting Statistics (summaryStatistics())
summaryStatistics())3.5 Boxing Back to Stream<Integer> (boxed())
Stream<Integer> (boxed())Working with Collectors (Collectors Utility Class)
Collectors (Collectors Utility Class)1. Collecting to List, Set, and Map
List, Set, and MapCollecting Stream Elements into a List
ListCollecting Stream Elements into a Set
SetCollecting Stream Elements into a Map
Map2. Grouping and Partitioning Data
Grouping Elements using groupingBy()
groupingBy()Partitioning Elements using partitioningBy()
partitioningBy()3. Summarizing Data with Collectors
CollectorsSumming Elements using summingInt()
summingInt()Calculating Average using averagingInt()
averagingInt()Getting Summary Statistics using summarizingInt()
summarizingInt()Last updated