Skip to content
This repository was archived by the owner on Feb 10, 2021. It is now read-only.

Commit 836a199

Browse files
authored
Create README.MD
- added Stream API tutorial
1 parent f1c5262 commit 836a199

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

stream-api/README.MD

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# <img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/image/logo_transparent_background.png" height=50/> Stream API tutorial
2+
3+
This is the tutorial on Stream API and functional programming techniques
4+
### Pre-conditions :heavy_exclamation_mark:
5+
You're supposed to be familiar with OOP, have basic knowledge of JDK, and be able to write Java code.
6+
### Related exercises :muscle:
7+
*
8+
### See also :point_down:
9+
* [Tutorial on Lambdas](https://github.com/bobocode-projects/java-functional-features-tutorial/tree/master/lambdas)
10+
* [Tutorial on Optional](https://github.com/bobocode-projects/java-functional-features-tutorial/tree/master/optional)
11+
##
12+
*Stream API* provide an ability to **process sequences of data elements in a declarative way** and **simplify the task of
13+
performing operations in parallel**
14+
15+
The simplest example is the task of filtering a collection. Using **imperative** old-style approach we specify **HOW the task
16+
should be done**. E.g. dealing with iteration, and storing each element in a new `ArrayList`. This way of processing is also
17+
called *external iteration*. *Stream API* and it's **declarative** approach allows us to specify **WHAT should be done**, without
18+
actually dealing with iteration and elements. This approach is also called *internal iteration*.
19+
20+
Imperative style:
21+
```java
22+
List<Account> gmailAccounts = new ArrayList<>();
23+
for (Account account : accounts) {
24+
if (account.getEmail().endsWith("@gmail.com")) {
25+
gmailAccounts.add(account);
26+
}
27+
}
28+
```
29+
Declarative style using *Stream API*:
30+
```java
31+
List<Account> gmailAccounts = accounts.stream()
32+
.filter(a -> a.getEmail().endsWith("@gmail"))
33+
.collect(toList());
34+
```

0 commit comments

Comments
 (0)