Skip to content

Commit 61c64f5

Browse files
Feature: Added notes on parallel stream in the readme file.
1 parent f6b800c commit 61c64f5

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

README.md

+21-2
Original file line numberDiff line numberDiff line change
@@ -212,15 +212,34 @@ provides a clear and concise way to represent one method interface using an expr
212212

213213
## Explanation Stream
214214

215-
A sequence of elements supporting sequential and parallel aggregate operations. To perform a computation, stream operations are composed into a __stream pipeline__. A stream pipeline consists of a _source_ (which might be an array, a collection, a generator function, an I/O channel, etc), zero or more intermediate operations (which transform a stream into another stream, such as _filter(Predicate)_), and a terminal operation (which produces a result or side-effect, such as _count()_ or _forEach(Consumer)_).
215+
**Stream()**: A sequence of elements supporting sequential and parallel aggregate operations. n simple words, a stream is a very nice, fancy iterator. Characteristics of a stream: sized, ordered/unordered, distinct, sorted.
216+
217+
To perform a computation, stream operations are composed into a __stream pipeline__. A stream pipeline consists of a _source_ (which might be an array, a collection, a generator function, an I/O channel, etc), zero or more intermediate operations (which transform a stream into another stream, such as _filter(Predicate)_), and a terminal operation (which produces a result or side-effect, such as _count()_ or _forEach(Consumer)_).
216218

217219
Streams are __lazy__; computation on the source data is __only performed when the terminal operation is initiated__, and source elements are consumed only as needed.
218220

219221
**Reference:** The source code for the Stream is present in [_Stream.java](https://github.com/syedumerahmedcode/java-functional-programming/blob/master/src/main/java/com/umer/javafunctional/streams/_Stream.java) class.
220222

221223
## Explanation Parallel Stream
222224

223-
- To be defined
225+
In simple words, when a parallel stream is executed, multiple threads are created that work on the stream pipeline.
226+
227+
For example,
228+
229+
```java
230+
231+
System.out.println("The end result with declarative approach WITH PARALLEL STREAM plus call of method is: "+
232+
numbers.parallelStream()
233+
.filter(e -> e%2==0)
234+
.mapToInt(e -> Sample.compute(e))
235+
.sum())
236+
);
237+
238+
```
239+
240+
Due to the nature of parallel stream, computations may not behave in intended way due to multiple threads being present. Hence, use it with _caution._
241+
242+
**Reference:** The source code for the Parallel Stream is present in [Sample.java](https://github.com/syedumerahmedcode/java-functional-programming/blob/master/src/main/java/com/umer/javafunctional/streams/Sample.java) class.
224243

225244
## Explanation Combinator Pattern
226245

notes.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ These are coming from Stream API.
1616
**Combinator Pattern**: This pattern is very useful for doing validation as it helps increase as well as decrease the validation on an object within the business logic. It is implemented inside _combinatorpattern_ package within _CustomerRegistrationValidator_ interface which extends Function<Customer, ValidationResult> where _Customer_ is the business object on which validation is applied whereas _ValidationResult_ is an enum which indicates the possible outcomes of the validation. **TODO**: Write separate section on **CustomerRegistrationValidator** interface.
1717
The combinator pattern is used in the Main class inside combinatorpattern package.
1818

19-
**Stream()**: In simple words, a stream is a very nice, fancy iterator.
20-
Characteristics of a stream: sized, ordered/unordered, distinct, sorted.
19+
2120
**filter()**: Like the names suggests, this is only used for filtering. This is a replacement of if statement from imperative programming. Parameter: Stream<T> takes Predicate<T>.
2221
**map()**: Map is a transforming function i.e. it transforms values. Here, number of output == number of input. Please note that there is no guarantee on the type of the output with respect to the type of the input. Parameter: Stream<T> map takes Function<T,R> to return Stream<R>.
2322

0 commit comments

Comments
 (0)