Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/main/java/data/Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ public static Employee generateEmployee() {
}

public static List<Employee> generateEmployeeList() {
// TODO
throw new UnsupportedOperationException();
return Stream.generate(Generator::generateEmployee)
.limit(10)
.collect(toList());
}
}
56 changes: 47 additions & 9 deletions src/test/java/part1/exercise/StreamsExercise1.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package part1.exercise;

import data.Employee;
import data.Generator;
import data.JobHistoryEntry;
import data.Person;
import org.junit.Test;
Expand All @@ -15,6 +16,9 @@
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;

import javax.naming.ldap.LdapName;

public class StreamsExercise1 {
// https://youtu.be/kxgo7Y4cdA8 Сергей Куксенко и Алексей Шипилёв — Через тернии к лямбдам, часть 1
Expand All @@ -25,16 +29,49 @@ public class StreamsExercise1 {

@Test
public void getAllEpamEmployees() {
List<Person> epamEmployees = null;// TODO all persons with experience in epam
throw new UnsupportedOperationException();
// TODO all persons with experience in epam
final List<Employee> employeeList = Generator.generateEmployeeList();

List<Person> actualPersonList = employeeList.stream()
.filter(employee -> employee.getJobHistory().stream()
.map(JobHistoryEntry::getEmployer)
.anyMatch("epam"::equalsIgnoreCase)
)
.map(Employee::getPerson)
.collect(toList());

List<Person> expectedPersonList = new ArrayList<>();
for (Employee employee : employeeList) {
for (JobHistoryEntry historyEntry : employee.getJobHistory()) {
if (historyEntry.getEmployer().equalsIgnoreCase("epam")) {
expectedPersonList.add(employee.getPerson());
break;
}
}
}
assertEquals(actualPersonList.size(), expectedPersonList.size());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can compare collections. assertEquals(actualPersonList, expectedPersonList)
You can use Sets to ignore order.

}

@Test
public void getEmployeesStartedFromEpam() {
List<Person> epamEmployees = null;// TODO all persons with first experience in epam
throw new UnsupportedOperationException();
// TODO all persons with first experience in epam
final List<Employee> employeeList = Generator.generateEmployeeList();
List<Person> epamEmployees = employeeList.stream()
.filter(e -> e.getJobHistory().stream().limit(1).collect(toList()).get(0).getEmployer().equals("epam"))
.map(Employee::getPerson)
.collect(Collectors.toList());
List<Person> expectedList = new ArrayList<>();

for (Employee anEmployeeList : employeeList) {
if (anEmployeeList.getJobHistory().get(0).getEmployer().equalsIgnoreCase("epam")) {

expectedList.add(anEmployeeList.getPerson());
}
}
assertEquals(expectedList, epamEmployees);
}


@Test
public void sumEpamDurations() {
final List<Employee> employees = generateEmployeeList();
Expand All @@ -49,11 +86,12 @@ public void sumEpamDurations() {
}
}

// TODO
throw new UnsupportedOperationException();

// int result = ???
// assertEquals(expected, result);
final int result = employees.stream()
.flatMap(employee -> employee.getJobHistory().stream())
.filter(j -> j.getEmployer().equals("epam"))
.mapToInt(JobHistoryEntry::getDuration)
.sum();
assertEquals(expected, result);
}

}
Loading