Skip to content

Commit 0abf443

Browse files
committed
Explain the flatmap operation. The hints.md and introduction.md files are not over yet.
1 parent fbe3f46 commit 0abf443

File tree

6 files changed

+101
-22
lines changed

6 files changed

+101
-22
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Hints
2+
3+
## 1.- Print the name of all the employees
4+
5+
WIP
6+
7+
8+
## 2.- Print the name and department of a given employee
9+
10+
WIP
11+

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,13 @@
33
In this exercise you will be writing code to print all the names of the factory employees.
44

55
Employees have an ID, a name and a department name, like in [tim-from-marketing](/exercises/concept/tim-from-marketing).
6-
Assume that the ID of the first employee is 1, the ID of the second employee is 2, and so on. If an employee has an ID, the other two fields, name and department name, have valid values.
6+
Assume that the ID of the first employee is 1, the ID of the second employee is 2, and so on. The three fields of an employee may be empty, That's why they are declared as Optional<T> types.
77

88
Two methods are already implemented:
99

1010
- `getAllTheEmployeesById()` returns an Optional<List<Employee>> object. Notice this method does NOT receive any parameter.
1111
- `getEmployeeById(id)` returns an Optional<Employee> object for the given ID, being Employee the following class:
1212

13-
```java
14-
class Employee {
15-
private int id;
16-
private String name;
17-
private String departmentName;
18-
// Getters and setters
19-
}
20-
```
21-
2213
## 1.- Print the names of all the employees
2314

2415
Implement the `printAllEmployeesNamesById()` method to print the names of all the employees, together with their id. If the employee does not exist, print "[id] - This employee does not exist".
@@ -42,4 +33,3 @@ printEmployeeNameAndDepartmentById(3) => "3 - Steve - Engineering"
4233
printEmployeeNameAndDepartmentById(4) => "4 - This employee does not exist"
4334
printEmployeeNameAndDepartmentById(5) => "5 - Charlotte - Owner"
4435
```
45-

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
## Introduction
66

7-
The Optional<T> type was introduced in Java 8 as a way to indicate that a method will return an object of type T or a null value. It is present in type signatures of many core Java methods.
7+
The **Optional<T>** type was introduced in Java 8 as a way to indicate that a method will return an object of type T or an empty value. It is present in type signatures of many core Java methods.
88

99
Before Java 8, developers had to implement null checks:
1010

1111
```java
1212
public Employee getEmployee(String name) {
13-
// Assume that getEmployeeByName retrieves an Employee from a data base
13+
// Assume that getEmployeeByName retrieves an Employee from a database
1414
Employee employee = getEmployeeByName(name);
1515
if (employee != null) {
1616
return employee;
@@ -46,10 +46,32 @@ Provided all the invoked methods return Optional objects, many methods can be ch
4646
public Optional<Integer> getEmployeeAge(String name) {
4747
Optional<Employee> optionalEmployee = getEmployeeByName(name);
4848
return getEmployeeByName(name)
49-
.map(Employee::getAge)
49+
.map(employee -> employee.getAge())
5050
.orElse(0);
5151
}
5252
```
5353

54-
It is important to understand that the Optional API does not eliminate the null values. It defers the null checking until the end of a series of methods, as long as all those methods return an optional object.
54+
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.
5555

56+
## Flatmap operation
57+
58+
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:
59+
60+
```java
61+
List<Optional<String>> listOfOptionals = Arrays.asList(
62+
Optional.of("Java"),
63+
Optional.empty(),
64+
Optional.of("Kotlin")
65+
);
66+
```
67+
68+
```java
69+
// Using flatMap to extract present values
70+
List<String> result = listOfOptionals.stream()
71+
.flatMap(Optional::stream)
72+
.collect(Collectors.toList());
73+
74+
System.out.println(result); // Output: [Java, Kotlin]
75+
}
76+
}
77+
```
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Introduction
22

3-
## Optional
3+
%{concept:optional-types}
44

5-
%{concept:optional}
5+
%{concept:flatMap-operation}
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
{
2-
"authors": ["josealonso"],
2+
"authors": [
3+
"josealonso"
4+
],
35
"files": {
46
"solution": [
5-
"src/main/java/TimFromMarketing2.java"
7+
"src/main/java/EmployeeService.java"
68
],
79
"test": [
8-
"src/test/java/TimFromMarketing2Test.java"
10+
"src/test/java/EmployeeServiceTest.java"
11+
],
12+
"exemplar": [
13+
".meta/src/reference/java/EmployeeService.java"
914
],
10-
"exemplar": []
1115
},
12-
"blurb": ""
16+
"icon": "language-list",
17+
"blurb": "Learn to use the Optional class by helping Tim print details of his company employees."
1318
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Design
2+
3+
## Goal
4+
5+
The goal of this exercise is to teach the student how to use the Optional API.
6+
We will use the most common methods: `ifPresent`, `orElse`, `ifPresentOrElse`, `orElseThrown`.
7+
The `isPresent` and `get` methods are not presented, since they do not provide any value over an ordinary null check.
8+
9+
Some methods of the Stream API are needed. This is a bit problematic, since they have not been explained in the current Java track.
10+
11+
## Learning objectives
12+
13+
- Know what optional types are.
14+
- Know how to use Optional<T> fields.
15+
- Know how to use methods that return an Optional<T> type.
16+
- See the utility of some Stream methods, `flatMap` specifically.
17+
18+
## Out of scope
19+
20+
- Streams API.
21+
22+
## Concepts
23+
24+
This Concepts Exercise's Concepts are:
25+
26+
- `Optional<T>` class and some methods that mimic a null check.
27+
28+
## Prerequisites
29+
30+
This Concept Exercise's prerequisites Concepts are:
31+
32+
- `custom classes`.
33+
- `generic-types`.
34+
- `streams`.
35+
36+
## Analyzer
37+
38+
wip
39+
40+
This exercise could benefit from the following rules in the [analyzer]:
41+
42+
- `actionable`: If the solution did not use `contains` in the method `containsLanguage`, instruct the student to do so.
43+
- `actionable`: If the solution did not use `isEmpty` in the method `isEmpty`, instruct the student to do so.
44+
- `informative`: If the student did not reuse the implementation of the `containsLanguage` method in the `isExciting` method, instruct them to do so.
45+
Explain that reusing existing code instead of copy-pasting can help make code easier to maintain.
46+
- `informative`: If the solution uses an `if statement` in the `containsLanguage` method, instruct the student to return directly the `contains` method.
47+
48+
If the solution does not receive any of the above feedback, it must be exemplar.
49+
Leave a `celebratory` comment to celebrate the success!
50+
51+
[analyzer]: https://github.com/exercism/java-analyzer

0 commit comments

Comments
 (0)