Skip to content

Commit d73177f

Browse files
committed
lint fixes
1 parent f85fdc1 commit d73177f

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

content/blog/java-streams.md

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Immutability ensures that the original data stays the same during stream operati
2727
- **Thread Safety:** In places with lots of things happening at once (multi-threaded), immutability acts like a safety net. Many threads can use the original data at the same time without causing problems.
2828

2929
**Code Snippet and Benchmark Output:**
30+
3031
```java
3132
// Immutability with Streams
3233
List<Integer> values = Arrays.asList(1, 10, 3, 4, 2);
@@ -41,12 +42,15 @@ System.out.println("Original List: " + values);
4142
System.out.println("Modified List: " + modifiedList);
4243
System.out.println("The above two lists are equal? " + (mappedValues.count() == values.size()));
4344
```
45+
4446
**Analysis:**
47+
4548
- The starting list stays the same; nothing is changed, proving that streams keep things unchanged.
4649
- When we filter and map, it's like making a fresh stream, leaving the original data intact.
4750
- Using `toList()` helps make a new list without messing up anything. It's like a safety feature for threads.
4851

4952
**Benchmark Output:**
53+
5054
```
5155
Original List: [1, 10, 3, 4, 2]
5256
Modified List: [11, 5]
@@ -59,10 +63,12 @@ Lists Equality: true
5963
Streams, once consumed, cannot be reused. This design ensures that each stream operation, especially terminal operations, represents a distinct processing step. Once a terminal operation is invoked on a stream, it marks the end of its usability for further operations.
6064

6165
**How it Benefits:**
66+
6267
- **Efficient Resource Management:** When you have a large amount of data, utilizing streams in Java aligns with the practice of processing data and closing resources promptly. This practice helps prevent data leakage and minimizes the presence of unused resources.
6368
- **Prevention of Unintended Behavior:** The inability to reuse streams after terminal operations minimizes the risk of unintended side effects, promoting safer and more controlled data manipulation. Attempting to use a consumed stream again results in an `IllegalStateException`, ensuring a clear and intentional data processing flow.
6469

6570
**Code Snippet and Benchmark Output:**
71+
6672
```java
6773
// Creating a stream from the list
6874
Stream<Integer> mappedValues = list.stream()
@@ -80,14 +86,17 @@ try {
8086
System.out.println("Error: " + e.getMessage());
8187
}
8288
```
89+
8390
**Analysis:**
91+
8492
- The code begins by creating a stream, `mappedValues`, from the original list.
8593
- The stream is processed with an intermediate operation (`filter` and `map`) and a terminal operation (`count`).
8694
- Attempting to reuse the `mappedValues` stream (here with `toList()`) after the terminal operation will result in an `IllegalStateException`. This error occurs because once a stream is consumed by a terminal operation, it cannot be reused for another terminal operation.
8795
- The use of `count()` serves as a point of consumption, making the stream no longer available for further terminal operations.
8896

8997
**Output:**
90-
```
98+
99+
```shell
91100
Error: stream has already been operated upon or closed
92101
```
93102

@@ -97,10 +106,12 @@ Error: stream has already been operated upon or closed
97106
Parallel streams divide the workload among multiple threads, leveraging the power of multicore processors for concurrent execution. They are designed for efficient processing of large datasets.
98107

99108
**How it Benefits:**
109+
100110
- **Reduced Execution Time:** Parallel processing leads to shorter execution times compared to sequential streams, especially when dealing with massive datasets.
101111
- **Optimal Resource Utilization:** Multicore processors are fully utilized, making parallel streams ideal for performance optimization.
102112

103113
**Code Snippet and Benchmark Output:**
114+
104115
```java
105116
// Populate the list with values
106117
for (int i = 0; i < datasetSize; i++) {
@@ -125,24 +136,27 @@ values.stream()
125136
long sequentialEndTime = System.currentTimeMillis();
126137
long sequentialTimeTaken = sequentialEndTime - sequentialStartTime;
127138
```
139+
128140
**Scenario 1: Small Dataset (datasetSize = 10) Analysis:**
141+
129142
- The dataset is deliberately kept small to showcase the impact on performance.
130143
- Parallel processing does not demonstrate a significant advantage due to the overhead of managing parallel threads which overshadows the potential advantages.
131144

132145
**Benchmark Output:**
146+
133147
```
134148
Parallel Time Taken: 9 milliseconds
135149
Sequential Time Taken: 0 milliseconds
136150
```
137151

138-
**Scenario 2: Large Dataset
152+
**Scenario 2: Large Dataset (datasetSize = 1,000,000) Analysis:**
139153

140-
(datasetSize = 1,000,000) Analysis:**
141154
- Parallel streams potentially perform better when dealing with large datasets.
142155
- The process of dividing the work among multiple threads becomes more advantageous for significant workloads.
143156
- Multicore processors are utilized optimally, leading to shorter execution times compared to sequential streams.
144157

145158
**Benchmark Output:**
159+
146160
```
147161
Parallel Time Taken: 2318 milliseconds
148162
Sequential Time Taken: 7789 milliseconds
@@ -159,6 +173,7 @@ In Java, the use of parallel streams can significantly improve performance for c
159173
- When multiple threads execute `println()` statements, the output becomes sequential.
160174

161175
**Example:**
176+
162177
```java
163178
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
164179

@@ -176,6 +191,7 @@ list.forEach(value -> {
176191
- The synchronous nature of the resource limits the full potential of parallelization.
177192

178193
**Example:**
194+
179195
```java
180196
// Limited impact with parallel streams
181197
System.out.println("\nParallel Stream:");
@@ -196,10 +212,12 @@ list.parallelStream()
196212
Lazy evaluation means that operations on a stream are not executed until a terminal operation is invoked. This allows for efficient resource usage by processing only the elements necessary for the final result.
197213

198214
**How it Benefits:**
215+
199216
- **Efficient Resource Utilization:** Elements are processed on-demand, saving processing time and memory.
200217
- **Suitable for Large Datasets:** Laziness ensures that the stream doesn't process the entire dataset if it's not required for the final result.
201218

202219
**Code Snippet and Benchmark Output:**
220+
203221
```java
204222
// Leveraging Lazy Evaluation
205223
List<Integer> values = Arrays.asList(1, 2, 5, 10, 15);
@@ -210,13 +228,16 @@ Stream<Integer> doubleValues = values.stream()
210228

211229
System.out.println(doubleValues.findFirst().orElse(0));
212230
```
231+
213232
**Analysis:**
233+
214234
- The stream operations, `filter` and `map`, are defined but not immediately executed.
215235
- Processing occurs only when the terminal operation `findFirst()` is invoked.
216236
- The lazy nature ensures that only elements needed for the final result are processed, saving resources.
217237
- The output demonstrates the selective processing of elements that meet the specified criteria.
218238

219239
**Output:**
240+
220241
```
221242
in isDivisibleBy5 1
222243
in isDivisibleBy5 2

0 commit comments

Comments
 (0)