Skip to content

part1 completed #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
9 changes: 9 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
<scope>provided</scope>
</dependency>


<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/data/Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ 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(() -> new Employee(generatePerson(), generateJobHistory()))
.limit(length)
.collect(toList());
}
}
40 changes: 30 additions & 10 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 @@ -12,9 +13,9 @@
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 java.util.stream.Collectors.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class StreamsExercise1 {
// https://youtu.be/kxgo7Y4cdA8 Сергей Куксенко и Алексей Шипилёв — Через тернии к лямбдам, часть 1
Expand All @@ -25,14 +26,30 @@ 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> employees = Generator.generateEmployeeList();
List<Person> epamEmployees = employees.stream()
.filter(e -> e.getJobHistory().stream().anyMatch(j -> j.getPosition().equals("epam")))
.map(Employee::getPerson)
.collect(Collectors.toList());

assertTrue(epamEmployees.stream()
.allMatch(p -> employees.stream().anyMatch(e ->
e.getPerson().equals(p) && e.getJobHistory().stream().anyMatch(j -> j.getPosition().equals("epam")))));
}

@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> employees = Generator.generateEmployeeList();
List<Person> epamEmployees = employees.stream()
.filter(e -> e.getJobHistory().get(0).getPosition().equals("epam"))
.map(Employee::getPerson)
.collect(Collectors.toList());

assertTrue(epamEmployees.stream()
.allMatch(p -> employees.stream().anyMatch(e ->
e.getPerson().equals(p) && e.getJobHistory().get(0).getPosition().equals("epam"))));
}

@Test
Expand All @@ -50,10 +67,13 @@ public void sumEpamDurations() {
}

// TODO
throw new UnsupportedOperationException();
int actual = employees.stream()
.flatMap(e -> e.getJobHistory().stream())
.filter(j -> j.getEmployer().equals("epam"))
.mapToInt(JobHistoryEntry::getDuration)
.sum();

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

}
81 changes: 70 additions & 11 deletions src/test/java/part1/exercise/StreamsExercise2.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
import data.Employee;
import data.JobHistoryEntry;
import data.Person;
import lombok.AllArgsConstructor;
import lombok.Data;
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.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

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

// TODO class PersonEmployerPair

@Data
@AllArgsConstructor
private static class Pair<F, S> {
private F first;
private S second;
}

@Test
public void employersStuffLists() {
Map<String, List<Person>> employersStuffLists = null;// TODO
throw new UnsupportedOperationException();
Map<String, List<Person>> expected = new HashMap<>();
for (Employee e : getEmployees()){
e.getJobHistory().forEach(j -> {
final String employer = j.getEmployer();
if (expected.containsKey(employer))
expected.get(employer).add(e.getPerson());
else {
List<Person> list = new ArrayList<>();
list.add(e.getPerson());
expected.put(employer, list);
}
});
}

// TODO
Map<String, List<Person>> actual = getEmployees().stream()
.flatMap(e -> e.getJobHistory().stream()
.map(j -> new Pair<>(j.getEmployer(), e.getPerson())))
.collect(groupingBy(
Pair::getFirst,
mapping(Pair::getSecond, toList())
));
assertThat(actual, is(expected));
}

@Test
public void indexByFirstEmployer() {
Map<String, List<Person>> employeesIndex = null;// TODO
throw new UnsupportedOperationException();
Map<String, List<Person>> expected = new HashMap<>();
for (Employee e : getEmployees()){
if (e.getJobHistory().size() > 0) {
final String firstEmployer = e.getJobHistory().get(0).getEmployer();
if (expected.containsKey(firstEmployer))
expected.get(firstEmployer).add(e.getPerson());
else {
List<Person> list = new ArrayList<>();
list.add(e.getPerson());
expected.put(firstEmployer, list);
}
}
}

// TODO
Map<String, List<Person>> actual = getEmployees().stream()
.flatMap(e -> e.getJobHistory().stream().limit(1)
.map(j -> new Pair<>(j.getEmployer(), e.getPerson())))
.collect(groupingBy(
Pair::getFirst,
mapping(Pair::getSecond, toList())
));
assertThat(actual, is(expected));
}

@Test
public void greatestExperiencePerEmployer() {
Map<String, Person> employeesIndex = null;// TODO
// TODO
Map<String, Person> employeesIndex = getEmployees().stream()
.flatMap(e -> e.getJobHistory().stream().limit(1)
.map(j -> new Pair<>(new Pair<>(j.getEmployer(), e.getPerson()), j.getDuration())))
.collect(
groupingBy(
t -> t.getFirst().getFirst(),
collectingAndThen(
maxBy(Comparator.comparingInt(Pair::getSecond)),
p -> p.get().getFirst().getSecond()
)
));

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