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
7 changes: 5 additions & 2 deletions src/main/java/data/Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ public static Employee generateEmployee() {
}

public static List<Employee> generateEmployeeList() {
// TODO
throw new UnsupportedOperationException();
int maxLength = 10;
final int length = ThreadLocalRandom.current().nextInt(maxLength) + 1;
return Stream.generate(Generator::generateEmployee)
.limit(length)
.collect(toList());
}
}
70 changes: 51 additions & 19 deletions src/test/java/part1/exercise/StreamsExercise1.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@
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.List;

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
Expand All @@ -25,20 +21,56 @@ public class StreamsExercise1 {

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

List<Person> expected = new ArrayList<>();

for (Employee e : employees) {
for (JobHistoryEntry j : e.getJobHistory()) {
if (j.getEmployer().equals("epam")) {
expected.add(e.getPerson());
break;
}
}
}
List<Person> actual = employees.stream()
.filter(e -> e.getJobHistory().stream()
.map(JobHistoryEntry::getEmployer)
.anyMatch("epam"::equals))
.map(Employee::getPerson)
.collect(toList());

assertEquals(expected, actual);
}

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

List<Person> expected = new ArrayList<>();

for (Employee e : employees) {
if (isFirstEmployerEpam(e))
expected.add(e.getPerson());
}
List<Person> actual = employees.stream()
.filter(StreamsExercise1::isFirstEmployerEpam)
.map(Employee::getPerson)
.collect(toList());

assertEquals(expected, actual);
}

private static boolean isFirstEmployerEpam(Employee e) {
return e.getJobHistory()
.stream()
.limit(1)
.anyMatch("epam"::equals);
}

@Test
public void sumEpamDurations() {
final List<Employee> employees = generateEmployeeList();

int expected = 0;

for (Employee e : employees) {
Expand All @@ -48,12 +80,12 @@ public void sumEpamDurations() {
}
}
}

// TODO
throw new UnsupportedOperationException();

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

}
95 changes: 80 additions & 15 deletions src/test/java/part1/exercise/StreamsExercise2.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@
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.stream.Stream;
import java.util.*;

import static data.Generator.generateEmployeeList;
import static java.util.stream.Collectors.*;
import static org.junit.Assert.assertEquals;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;

public class StreamsExercise2 {
// https://youtu.be/kxgo7Y4cdA8 Сергей Куксенко и Алексей Шипилёв — Через тернии к лямбдам, часть 1
Expand All @@ -22,25 +18,94 @@ public class StreamsExercise2 {
// https://youtu.be/O8oN4KSZEXE Сергей Куксенко — Stream API, часть 1
// https://youtu.be/i0Jr2l3jrDA Сергей Куксенко — Stream API, часть 2

// TODO class PersonEmployerPair
// TODO class PersonEmployerDuration
private static class PersonEmployerDuration
{
private Person person;
private String employer;
private Integer duration;

PersonEmployerDuration(Person p, String e) {
this(p,e,0);
}

PersonEmployerDuration(Person p, String e, Integer d) {
employer = e;
person = p;
duration = d;
}

Person getPerson() {
return person;
}

String getEmployer() {
return employer;
}

int getDuration() {
return duration;
}
}

@Test
public void employersStuffLists() {
Map<String, List<Person>> employersStuffLists = null;// TODO
throw new UnsupportedOperationException();
Map<String, List<Person>> employersStuffLists = getEmployees().stream()
.flatMap(e -> e.getJobHistory()
.stream()
.map(j -> new PersonEmployerDuration(
e.getPerson(),
j.getEmployer())))
.collect(groupingBy(
PersonEmployerDuration::getEmployer,
mapping(PersonEmployerDuration::getPerson, toList())));

assertThat(employersStuffLists.get("Microsoft"),
equalTo(Collections.singletonList(new Person("John", "White", 25))));
}

@Test
public void indexByFirstEmployer() {
Map<String, List<Person>> employeesIndex = null;// TODO
throw new UnsupportedOperationException();
Map<String, List<Person>> employeesIndex = getEmployees().stream()
.map(e -> new PersonEmployerDuration(
e.getPerson(),
e.getJobHistory().get(0).getEmployer()))

Choose a reason for hiding this comment

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

Avoid get.

.collect(groupingBy(PersonEmployerDuration::getEmployer,
mapping(PersonEmployerDuration::getPerson, toList())));

assertThat(employeesIndex.get("abc"), equalTo(null));
assertThat(employeesIndex.get("yandex"), equalTo(Arrays.asList(
new Person("John", "Doe", 21),
new Person("John", "Doe", 24),
new Person("Bob", "Doe", 27),
new Person("John", "Doe", 30))));
}

private static Map<String, Integer> jobHistory(List<JobHistoryEntry> jobHistory){
return jobHistory.stream()
.collect(groupingBy(
JobHistoryEntry::getEmployer,
summingInt(JobHistoryEntry::getDuration)));
}

@Test
public void greatestExperiencePerEmployer() {
Map<String, Person> employeesIndex = null;// TODO
Map<String, Person> employeesIndex = getEmployees().stream()
.flatMap(e -> jobHistory(e.getJobHistory())
.entrySet().stream()
.map(j -> new PersonEmployerDuration(
e.getPerson(),
j.getKey(),
j.getValue()))
)
.collect(groupingBy(
PersonEmployerDuration::getEmployer,
collectingAndThen(maxBy(Comparator.comparingInt(PersonEmployerDuration::getDuration)),
p->p.get().getPerson()))
);


assertEquals(new Person("John", "White", 28), employeesIndex.get("epam"));
assertThat(new Person("John", "White", 28), equalTo(employeesIndex.get("epam")));
}


Expand Down Expand Up @@ -80,7 +145,7 @@ private List<Employee> getEmployees() {
new Employee(
new Person("John", "White", 25),
Collections.singletonList(
new JobHistoryEntry(6, "QA", "epam")
new JobHistoryEntry(6, "QA", "Microsoft")
)),
new Employee(
new Person("John", "Galt", 26),
Expand Down
Loading