forked from javakurssi/Tuntimateriaalit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptionalExamples.java
More file actions
68 lines (55 loc) · 2.05 KB
/
Copy pathOptionalExamples.java
File metadata and controls
68 lines (55 loc) · 2.05 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
65
66
67
68
package lesson5;
import java.util.List;
import java.util.Optional;
import lesson5.helpers.City;
/**
* The purpose of this example is to demonstrate usage of the Optional object.
*/
public class OptionalExamples {
public static void main(String[] args) {
List<City> cities = List.of(
new City("Helsinki", 500_000),
new City("Tampere", 200_000),
new City("Pori", 70_000));
City helsinki = findByName(cities, "Helsinki");
System.out.println("Population of Helsinki: " + helsinki.getPopulation());
City turku = findByName(cities, "Turku");
// What's the problem here?
System.out.println("Population of Turku: " + turku.getPopulation());
// Optional object, either contains City object or is empty
Optional<City> maybeTurku = maybeFindByName(cities, "Turku");
// Optional communicates the possibility of a null (or "empty") value
if (maybeTurku.isPresent()) {
System.out.println("Population of Turku: " + maybeTurku.get().getPopulation());
}
int populationOfTurku = maybeTurku
.map((city) -> city.getPopulation())
.orElse(0);
System.out.println("Population of Turku: " + populationOfTurku);
}
/*
* First implementation: return a City object if found, otherwise return null
*/
private static City findByName(List<City> cities, String cityName) {
for (City city : cities) {
if (city.getName().equals(cityName)) {
return city;
}
}
// Danger zone!
return null;
}
/*
* Second implementation: return an Optional object
*/
private static Optional<City> maybeFindByName(List<City> cities, String cityName) {
for (City city : cities) {
if (city.getName().equals(cityName)) {
// Return Optional with a value
return Optional.of(city);
}
}
// Return empty Optional
return Optional.empty();
}
}