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());
}
}
77 changes: 61 additions & 16 deletions src/test/java/part1/exercise/StreamsExercise1.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,79 @@

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 java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Predicate;

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.hamcrest.core.Is.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

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
private final static String EPAM = "epam";
private final Predicate<String> isEpam = s -> s.equals(EPAM);

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

List<Employee> initialEmployees = generateEmployeeList();
List<Employee> actual = initialEmployees.stream()
.filter(employee ->
employee.getJobHistory().stream()
.map(JobHistoryEntry::getEmployer)
.anyMatch(isEpam))
.collect(toList());

List<Employee> expected = new ArrayList<>();
for (Employee employee : initialEmployees) {
boolean isEpamEmloyee = false;
for (JobHistoryEntry entry : employee.getJobHistory()) {
if (isEpam.test(entry.getEmployer())) {
isEpamEmloyee = true;
}
}
if (isEpamEmloyee) expected.add(employee);
}
assertThat(actual, is(expected));
}

@Test
public void getEmployeesStartedFromEpam() {
List<Person> epamEmployees = null;// TODO all persons with first experience in epam
throw new UnsupportedOperationException();
List<Employee> initialList = generateEmployeeList();

List<Employee> actualList = initialList.stream()
.filter(employee -> employee.getJobHistory()
.stream()
.findFirst()
.map(JobHistoryEntry::getEmployer)
.filter(isEpam)
.map(than -> true)
.orElse(false))
.collect(toList());

List<Employee> expectedList = new ArrayList<>();
for (Employee employee : initialList) {
List<JobHistoryEntry> jobHistory = employee.getJobHistory();
boolean epamIsFirstEmployer = false;
if (!jobHistory.isEmpty()) {
String employer = jobHistory.get(0).getEmployer();
epamIsFirstEmployer = isEpam.test(employer);
}
if (epamIsFirstEmployer) {
expectedList.add(employee);
}
}
assertThat(actualList, is(expectedList));
}

@Test
Expand All @@ -49,11 +91,14 @@ public void sumEpamDurations() {
}
}

// TODO
throw new UnsupportedOperationException();
int actual = employees.stream()
.map(Employee::getJobHistory)
.flatMap(Collection::stream)
.filter(s -> isEpam.test(s.getEmployer()))
.mapToInt(JobHistoryEntry::getDuration)
.sum();

// int result = ???
// assertEquals(expected, result);
assertEquals(expected, actual);
}

}
135 changes: 121 additions & 14 deletions src/test/java/part1/exercise/StreamsExercise2.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,152 @@
import data.Person;
import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;

import static data.Generator.generateEmployeeList;
import static java.util.Comparator.comparingInt;
import static java.util.stream.Collectors.*;
import static org.junit.Assert.assertEquals;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

public class StreamsExercise2 {
// https://youtu.be/kxgo7Y4cdA8 Сергей Куксенко и Алексей Шипилёв — Через тернии к лямбдам, часть 1
// https://youtu.be/JRBWBJ6S4aU Сергей Куксенко и Алексей Шипилёв — Через тернии к лямбдам, часть 2

// https://youtu.be/O8oN4KSZEXE Сергей Куксенко — Stream API, часть 1
// https://youtu.be/i0Jr2l3jrDA Сергей Куксенко — Stream API, часть 2
private final static Predicate<Employee> hasJob = s -> !s.getJobHistory().isEmpty();

// TODO class PersonEmployerPair
private static class PersonEmployerPair {
private final Person person;
private final String employee;


private PersonEmployerPair(Person person, String employee) {
this.person = person;
this.employee = employee;
}

Person getPerson() {
return person;
}

String getEmployee() {
return employee;
}
}
@Test
public void employersStuffLists() {
Map<String, List<Person>> employersStuffLists = null;// TODO
throw new UnsupportedOperationException();
List<Employee> initialList = getEmployees();
Map<String, List<Person>> actualMap = initialList.stream()
.flatMap(this::toPersonEmployerPairStream)
.collect(groupingBy(PersonEmployerPair::getEmployee,
mapping(PersonEmployerPair::getPerson, toList())));
Map<String, List<Person>> expectedMap = new HashMap<>();

List<String> employers = Arrays.asList("epam", "yandex", "google", "abc");
for (String employer : employers) {
List<Person> personList = new ArrayList<>();
for (Employee employee : initialList) {
if (employee.getJobHistory().stream()
.map(JobHistoryEntry::getEmployer)
.anyMatch(s -> s.equals(employer))) {
personList.add(employee.getPerson());
}
}
expectedMap.put(employer, personList);
}

assertThat(actualMap, is(expectedMap));
}

private Stream<PersonEmployerPair> toPersonEmployerPairStream(Employee s) {
return s.getJobHistory().stream().map(g -> new PersonEmployerPair(s.getPerson(), g.getEmployer()));
}

@Test
public void indexByFirstEmployer() {
Map<String, List<Person>> employeesIndex = null;// TODO
throw new UnsupportedOperationException();
final List<Employee> initialList = getEmployees();

final Function<Employee, PersonEmployerPair> toPersonEmployerPair =
s -> new PersonEmployerPair(s.getPerson(), s.getJobHistory().get(0).getEmployer());

Map<String, List<Person>> actualMap = getEmployees().stream()
.filter(hasJob)
.map(toPersonEmployerPair)
.collect(groupingBy(PersonEmployerPair::getEmployee, mapping(PersonEmployerPair::getPerson, toList())));

Map<String, List<Person>> expectedMap = new HashMap<>();

List<String> employers = Arrays.asList("epam", "yandex", "google", "abc");
for (String employer : employers) {
List<Person> personList = new ArrayList<>();
for (Employee employee : initialList) {
if (employee.getJobHistory().stream()
.findFirst()
.map(JobHistoryEntry::getEmployer)
.filter(s -> s.equals(employer))
.map(s -> true)
.orElse(false)) {
personList.add(employee.getPerson());
}
}
if (!personList.isEmpty()) expectedMap.put(employer, personList);
}

assertThat(actualMap, is(expectedMap));
}

private static class PersonEmployerDuration {
private final Person person;
private final String employer;
private final int duration;

private PersonEmployerDuration(Person person, String employer, int duration) {
this.person = person;
this.employer = employer;
this.duration = duration;
}

Person getPerson() {
return person;
}

String getEmployer() {
return employer;
}

int getDuration() {
return duration;
}
}

@Test
public void greatestExperiencePerEmployer() {
Map<String, Person> employeesIndex = null;// TODO

assertEquals(new Person("John", "White", 28), employeesIndex.get("epam"));
List<Employee> initialList = getEmployees();
Map<String, Person> employeesIndex = initialList.stream()
.filter(hasJob)
.flatMap(this::toPersonEmployerDurationStream)
.collect(groupingBy(PersonEmployerDuration::getEmployer,
collectingAndThen(maxBy(comparingInt(PersonEmployerDuration::getDuration)),
g -> g.get().getPerson())));

Map<String, Person> expected = new HashMap<>();

expected.put("abc", new Person("John", "Doe", 30));
expected.put("yandex", new Person("John", "Doe", 21));
expected.put("epam", new Person("John", "White", 28));
expected.put("google", new Person("John", "Galt", 20));

assertThat(employeesIndex, is(expected));
}

private Stream<PersonEmployerDuration> toPersonEmployerDurationStream(Employee s) {
return s.getJobHistory().stream().map(j->new PersonEmployerDuration(s.getPerson(),j.getEmployer(),j.getDuration()));
}

private List<Employee> getEmployees() {
return Arrays.asList(
Expand Down Expand Up @@ -120,5 +228,4 @@ private List<Employee> getEmployees() {
))
);
}

}