4. Lambdas Expressions : Definition
Java - Functionalities
4
● Representation of one anonymous function
Anonymous : We say anonymous because it doesn't have an explicit name like a method
would normally have; less to write and think about
Function : We say function, because a lambda isn't associate with particular class like
method is. But like a method, a lambda has a list of parameters, a body, a return type, and a
possible list of exceptions that can be thrown.
Passed around : A lambda expression can be passed as argument to a method or stored in
a variable.
Concise : You do not need to write a lot of boilerplate.
Credits -> Definition from the book Modern Java in Action
5. Lambdas Expressions : Before and Later
Java - Functionalities
5
● Before
● Later
Lambda is composed by 3 parts:
Definition from the book Modern Java in Action
6. Lambdas Expressions : Quiz
Java - Functionalities
6
What lambdas expressions are not corrected?
Quiz Lambda Syntax
1. () -> {}
2. () -> “Yoda”
3. () -> { return “Jedi”; }
4. (Integer i) -> return “Jedi” + i;
5. (String s) -> { “Star Wars”; }
Definition from the book Modern Java in Action
7. Lambdas Expressions : Quiz
Java - Functionalities
7
What lambdas expressions are not corrected?
Quiz Lambda Syntax
1. () -> {}
2. () -> “Yoda”
3. () -> { return “Jedi”; }
4. (Integer i) -> return “Jedi” + i;
5. (String s) -> { “Star Wars”; }
Definition from the book Modern Java in Action
8. Lambdas Expressions : Why?
Java - Functionalities
8
Syntax
Why ?
It is necessary use braces*
The “Star Wars” is an expression, it is not a statement. There are two ways to do that.
(Integer i) -> return “Roan” + i;
(Integer i) -> { return “Roan”+ i;}
(String s) -> { “Star Wars’;}
(String s) -> “Star Wars”
(String s) -> { return “Star Wars”; }
Exception method void
process(() -> System.out.println(“Roan"));
Definition from the book Modern Java in Action
9. Lambdas Expressions : Some examples
Java - Functionalities
9
Definition from the book Modern Java in Action
10. Lambdas Expressions: Quiz
Java - Functionalities
10
Quelles sont les expressions lambdas correctes ?
Quiz Lambda Syntax
execute(() -> {});
public void execute(Runnable r) {
r.run();
}
public Callable<String> fetch(){
return () -> “Joker Exemple ☺”;
}
Predicate<Apple> p = (Apple a) -> a.getWeight();
Definition from the book Modern Java in Action
11. Lambdas Expressions : Quiz
Java - Functionalities
11
Quelles sont les expressions lambdas correctes ?
Quiz Lambda Syntax
execute(() -> {});
public void execute(Runnable r) {
r.run();
}
public Callable<String> fetch(){
return () -> “Joker Exemple ☺”;
}
Predicate<Apple> p = (Apple a) -> a.getWeight();
(Apple a) -> a.getWeight(); a de signature (Apple) -> Integer e le Predicate<Apple> attends (Apple) -> boolean
Definition from the book Modern Java in Action
12. Functional Interfaces : Definition and Examples
Java - Functionalities
12
● Functional Interface are interfaces with just an abstract method without or with the annotation
@FunctionalInterface
Definition from the book Modern Java in Action
13. Functional Interfaces: More Example
Java - Functionalities
13
Definition from the book Modern Java in Action
14. Functional Interfaces : Examples
Java - Functionalities
14
Definition from the book Modern Java in Action
15. Functional Interfaces : Examples
Java - Functionalities
15
●
Predicate<Integer> IntPredicate
Performance
Boxing
Unboxing
Definition from the book Modern Java in Action
17. Functional Interfaces: Quiz
Java - Functionalities
17
What are each functional interfaces below?
Quiz
1. T -> R
2. (int, int) -> int
3. T -> void
4. () -> T
5. (T, U) -> R
1. Function<T, R>
2. IntBinaryOperator
3. Consumer<T>
4. Supplier<T>
5. BiFunction<T, U, R>
Definition from the book Modern Java in Action
19. ● Example:
Method Reference
Java - Functionalities
19
Beneficiar de uma melhor leitura de código
Thanks for this operator « :: » (classe::méthode)
Define an abstract method from une functional interface
Definition from the book Modern Java in Action
21. API Stream
Java - Functionalities
21
package java.util.stream
https://github.com/roanbrasil/clean-code-tdc/blob/master/src/main/java/com/tdc/cleancode/stream_api/StreamsExample.java
Definition from the book Modern Java in Action
23. Classe Optional: Hadouken
Java - Functionalities
23
https://github.com/roanbrasil/clean-code-tdc/blob/master/src/main/java/com/tdc/cleancode/optional/before/BeforeOptional.java
24. Classe Optional: Hadouken
Java - Functionalities
24
https://github.com/roanbrasil/clean-code-tdc/blob/master/src/main/java/com/tdc/cleancode/optional/after/AfterOptional.java
25. ● Some util methods, one of we use a lot :
Classe Optional
Java - Functionalities
25
Optional.of (objet non nullable) Optional.ofNullable (objet nullable)
isPresent() isEmpty()
orElse() orElseThrow()
get()
filter()
map() flatMap()
26. API Date/Time
Java - Functionalities
26
Start from 1900
It's not a date
It is point in a time with some
precision in millisecondes
The months starts with the
index 0 a 11
Timezone CET (Central Europe Time)
Use a library externe like
JodaTime
DateFormat no thread-safe
Date et Dateformat are
mutable
27. ● There are few classes form new Api Date/Time, such as LocalDate, LocalDateTime, Instant, LocalTime, Duration and
Period.
● Example of code :
API Date/Time
Java - Functionalities
27
Definition from the book Modern Java in Action
28. ● Exemple de code :
API Date/Time
Java - Functionalities
28
Definition from the book Modern Java in Action
29. ● Example de code :
API Date/Time
Java - Functionalities
29
https://github.com/roanbrasil/clean-code-tdc/blob/master/src/main/java/com/tdc/cleancode/java_time_api/before/BeforeJava8JavaTimeApi.java
https://github.com/roanbrasil/clean-code-tdc/blob/master/src/main/java/com/tdc/cleancode/java_time_api/after/AfterJava8JavaTimeApi.java
30. ● Create direct methods from interfaces with the key word « default »
● Exemple :
public interface Step {
void execute (Information information);
default String fallback(){
return "ERRO";
}
}
Default Methods
Java - Functionalities
30