-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathStaticFactoryMethods.java
More file actions
64 lines (41 loc) · 1.39 KB
/
Copy pathStaticFactoryMethods.java
File metadata and controls
64 lines (41 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package dev.rinaldo.java9;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
/**
* JAVA 9 - Static Factory Methods de List, Set e Map
*
* @author youtube.com/RinaldoDev
*
* @SejaMembro e escolha o assunto do próximo vídeo!
*/
public class StaticFactoryMethods {
public static void main(String[] args) {
// Nomes com significado
Optional<Integer> empty = Optional.empty();
Optional<Integer> cemOpt = Optional.of(100);
Optional<Integer> vazio = Optional.ofNullable(null);
// Flexibilidade no retorno
List<Object> list = new ArrayList<>();
List<Object> novaLista = Collections.unmodifiableList(list);
// novaLista.add(1); lança exceção
// Controle das Instâncias
Integer cem = Integer.valueOf(100);
Integer duz = Integer.valueOf(200);
Integer tre = Integer.valueOf(300);
// Como fazia antes
List<Integer> list2 = Arrays.asList(cem, duz, tre);
// Collections.singletonList(o)
// Formato Novo no Java 9
List<Integer> of1 = List.of();
List<Integer> of2 = List.of(cem);
List<Integer> of3 = List.of(cem, duz);
List<Integer> of4 = List.of(cem, duz, tre);
// Imutável
// of1.add(duz);
// Map.of(k1, v1)
// Set.of(e1, e2)
}
}