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

Commit b0f936c

Browse files
authored
Update readme.md
- added lambda intro
1 parent f1b6eb3 commit b0f936c

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

lambdas/readme.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,25 @@ You're supposed to be familiar with OOP, have basic knowledge of JDK, and be abl
88
### See also :point_down:
99
* [Tutorial on Optional](https://github.com/bobocode-projects/java-8-tutorial/tree/master/optional)
1010
* [Tutorial on Stream API](https://github.com/bobocode-projects/java-8-tutorial/tree/master/stream-api)
11+
##
12+
Java is an OOP language, so it always works with classes and **doesn't support stanalone functions**. In case you want to **pass some function as a method parameter**, or **store some code into a variable**, you should use a *Funatinal Interface* and a *Lambda expression*.
13+
14+
* A *Functional Interface* represents a **function signature**. It contains only one abstract method.
15+
* A *Lambda expressoin* respresents a **function body**. Is an anonymous function that implements the abstract method of the functiona interface
16+
17+
The purpose of the lambda and functional interfaces is to **make it easier to create function objects** and provide an **ability to use some functional programming technics in Java.**
18+
19+
A typical example is interface `Comparator<T>`:
20+
21+
```
22+
accounts.sort(new Comparator<Account>() {
23+
@Override
24+
public int compare(Account o1, Account o2) {
25+
return o1.getFirstName().compareTo(o2.getFirstName());
26+
}
27+
});
28+
```
29+
It can be easily simplified using lambda expression:
30+
```
31+
accounts.sort((a1, a2) -> a1.getFirstName().compareTo(a2.getFirstName()));
32+
```

0 commit comments

Comments
 (0)