-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathStreamsExercise1.java
More file actions
executable file
·69 lines (57 loc) · 2.55 KB
/
StreamsExercise1.java
File metadata and controls
executable file
·69 lines (57 loc) · 2.55 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
69
package part1.exercise;
import data.Employee;
import data.JobHistoryEntry;
import data.Person;
import org.junit.Test;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import static data.Generator.generateEmployeeList;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.mapping;
import static java.util.stream.Collectors.toList;
import static org.junit.Assert.assertEquals;
public class StreamsExercise1 {
// https://youtu.be/kxgo7Y4cdA8 Сергей Куксенко и Алексей Шипилёв — Через тернии к лямбдам, часть 1
// https://youtu.be/JRBWBJ6S4aU Сергей Куксенко и Алексей Шипилёв — Через тернии к лямбдам, часть 2
// https://youtu.be/O8oN4KSZEXE Сергей Куксенко — Stream API, часть 1
// https://youtu.be/i0Jr2l3jrDA Сергей Куксенко — Stream API, часть 2
@Test
public void getAllEpamEmployees() {
List<Employee> employees = generateEmployeeList();
List<Employee> epamEmployees = employees.stream()
.filter(e -> e.getJobHistory().stream()
.anyMatch(s -> s.getEmployer().equals("epam")))
.collect(toList());
}
@Test
public void getEmployeesStartedFromEpam() {
List<Employee> epamEmployees = generateEmployeeList().stream()
.filter(e -> e.getJobHistory().stream()
.findFirst()
.map(JobHistoryEntry::getEmployer)
.map(employer -> employer.equals("epam"))
.orElse(false))
.collect(toList());
}
@Test
public void sumEpamDurations() {
final List<Employee> employees = generateEmployeeList();
int expected = 0;
for (Employee e : employees) {
for (JobHistoryEntry j : e.getJobHistory()) {
if (j.getEmployer().equals("epam")) {
expected += j.getDuration();
}
}
}
final int result = employees.stream()
.flatMap(e -> e.getJobHistory().stream()
.filter(s -> s.getEmployer().equals("epam")))
.mapToInt(JobHistoryEntry::getDuration)
.sum();
assertEquals(expected, result);
}
}