You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+21-2
Original file line number
Diff line number
Diff line change
@@ -212,15 +212,34 @@ provides a clear and concise way to represent one method interface using an expr
212
212
213
213
## Explanation Stream
214
214
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)_).
216
218
217
219
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.
218
220
219
221
**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.
220
222
221
223
## Explanation Parallel Stream
222
224
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.
Copy file name to clipboardExpand all lines: notes.md
+1-2
Original file line number
Diff line number
Diff line change
@@ -16,8 +16,7 @@ These are coming from Stream API.
16
16
**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.
17
17
The combinator pattern is used in the Main class inside combinatorpattern package.
18
18
19
-
**Stream()**: In simple words, a stream is a very nice, fancy iterator.
20
-
Characteristics of a stream: sized, ordered/unordered, distinct, sorted.
19
+
21
20
**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>.
22
21
**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>.
0 commit comments