Skip to content

Commit 9d4cd39

Browse files
committed
Delete explanation about flatMap.
1 parent 4b7a800 commit 9d4cd39

File tree

1 file changed

+4
-25
lines changed

1 file changed

+4
-25
lines changed

exercises/concept/tim-from-marketing-2/.docs/introduction.md

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -63,28 +63,7 @@ public Optional<Integer> getEmployeeAge(String name) {
6363
}
6464
```
6565

66-
It is important to understand that the Optional API does not eliminate the null checking, but it defers it until the end of a series of methods, as long as all those methods return an optional object.
67-
The fields of the Employee class have an Optional type. Notice that this is not recommended, as explained by one well-known Java language architect in [this SO answer](https://stackoverflow.com/questions/26327957/should-java-8-getters-return-optional-type)
68-
69-
## Flatmap operation
70-
71-
The **flatMap** method flattens a List of Optional objects into a List of those objects. In other words, extracts the value of each list element, discarding empty Optionals. For example:
72-
73-
```java
74-
List<Optional<String>> listOfOptionals = Arrays.asList(
75-
Optional.of("Java"),
76-
Optional.empty(),
77-
Optional.of("Kotlin")
78-
);
79-
```
80-
81-
```java
82-
// Using flatMap to extract present values
83-
List<String> result = listOfOptionals.stream()
84-
.flatMap(language -> language.stream())
85-
.collect(Collectors.toList());
86-
87-
System.out.println(result); // Output: [Java, Kotlin]
88-
}
89-
}
90-
```
66+
It is important to understand that the Optional API does not eliminate the null checking,
67+
but it defers it until the end of a series of methods, as long as all those methods return an optional object.
68+
The Optional type is mainly used as returned type. Using it as a parameter type or field type is less common and
69+
not recommended, as explained by one well-known Java language architect in [this SO answer](https://stackoverflow.com/questions/26327957/should-java-8-getters-return-optional-type)

0 commit comments

Comments
 (0)