From 2707a2d8a6f23b7d5b0f566da1d0baf9d0e83e3e Mon Sep 17 00:00:00 2001 From: Aleksandra Pankratova Date: Sat, 22 Apr 2017 20:32:33 +0300 Subject: [PATCH 1/3] - part1 is done --- src/main/java/data/Generator.java | 5 +- .../java/part1/exercise/StreamsExercise1.java | 46 ++++++-- .../java/part1/exercise/StreamsExercise2.java | 109 ++++++++++++++++-- 3 files changed, 139 insertions(+), 21 deletions(-) diff --git a/src/main/java/data/Generator.java b/src/main/java/data/Generator.java index cbd46ea..679bf70 100644 --- a/src/main/java/data/Generator.java +++ b/src/main/java/data/Generator.java @@ -60,7 +60,8 @@ public static Employee generateEmployee() { } public static List generateEmployeeList() { - // TODO - throw new UnsupportedOperationException(); + return Stream.generate(Generator::generateEmployee) + .limit(10) + .collect(toList()); } } diff --git a/src/test/java/part1/exercise/StreamsExercise1.java b/src/test/java/part1/exercise/StreamsExercise1.java index 02dd2da..9b7c786 100755 --- a/src/test/java/part1/exercise/StreamsExercise1.java +++ b/src/test/java/part1/exercise/StreamsExercise1.java @@ -15,6 +15,7 @@ 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 @@ -25,14 +26,40 @@ public class StreamsExercise1 { @Test public void getAllEpamEmployees() { - List epamEmployees = null;// TODO all persons with experience in epam - throw new UnsupportedOperationException(); + List epamList = generateEmployeeList();// all persons with experience in epam + List epamEmployees = epamList.stream() + .filter(e -> e.getJobHistory() + .stream() + .map(JobHistoryEntry::getEmployer) + .anyMatch(emp -> emp.equalsIgnoreCase("epam"))) + .map(Employee::getPerson) + .collect(toList()); + List expectedList = new ArrayList<>(); + epamList.forEach(e -> { + for (JobHistoryEntry job : e.getJobHistory()) { + if (job.getEmployer().equals("epam")) { + expectedList.add(e.getPerson()); + break; + } + } + }); + assertEquals(epamEmployees, expectedList); } @Test public void getEmployeesStartedFromEpam() { - List epamEmployees = null;// TODO all persons with first experience in epam - throw new UnsupportedOperationException(); + List emp = generateEmployeeList();// all persons with first experience in epam + List epamEmployees = emp.stream() + .filter(e -> e.getJobHistory().get(0).getEmployer().equalsIgnoreCase("epam")) + .map(Employee::getPerson) + .collect(toList()); + List expectedList = new ArrayList<>(); + emp.forEach(e -> { + if (e.getJobHistory().get(0).getEmployer().equalsIgnoreCase("epam")) { + expectedList.add(e.getPerson()); + } + }); + assertEquals(epamEmployees, expectedList); } @Test @@ -49,11 +76,12 @@ public void sumEpamDurations() { } } - // TODO - throw new UnsupportedOperationException(); - - // int result = ??? - // assertEquals(expected, result); + int result = employees.stream() + .flatMap(e -> e.getJobHistory().stream()) + .filter(emp -> emp.getEmployer().equalsIgnoreCase("epam")) + .mapToInt(JobHistoryEntry::getDuration) + .sum(); + assertEquals(expected, result); } } diff --git a/src/test/java/part1/exercise/StreamsExercise2.java b/src/test/java/part1/exercise/StreamsExercise2.java index 5be9d38..70c4278 100755 --- a/src/test/java/part1/exercise/StreamsExercise2.java +++ b/src/test/java/part1/exercise/StreamsExercise2.java @@ -5,13 +5,12 @@ 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.stream.Collectors; import java.util.stream.Stream; import static data.Generator.generateEmployeeList; +import static java.util.Comparator.comparing; import static java.util.stream.Collectors.*; import static org.junit.Assert.assertEquals; @@ -22,23 +21,113 @@ public class StreamsExercise2 { // https://youtu.be/O8oN4KSZEXE Сергей Куксенко — Stream API, часть 1 // https://youtu.be/i0Jr2l3jrDA Сергей Куксенко — Stream API, часть 2 - // TODO class PersonEmployerPair + // class PersonEmployerPair + private static class PersonEmployerPair { + private final Person person; + private final String employer; + + public PersonEmployerPair(Person person, String employer) { + this.person = person; + this.employer = employer; + } + + public Person getPerson() { + return person; + } + + public String getEmployer() { + return employer; + } + } + + private static class PersonEmployerDuration { + private final Person person; + private final String employer; + private final int duration; + + 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; + } + } + + private Stream employeesToPairs(Employee employee) { + return employee.getJobHistory().stream() + .map(JobHistoryEntry::getEmployer) + .map(e -> new PersonEmployerPair(employee.getPerson(), e)); + } + @Test public void employersStuffLists() { - Map> employersStuffLists = null;// TODO - throw new UnsupportedOperationException(); + final List employees = getEmployees(); + Map> employersStuffLists = employees.stream() + .flatMap(this::employeesToPairs) + .collect(Collectors.groupingBy( + PersonEmployerPair::getEmployer, + mapping(PersonEmployerPair::getPerson, toList()))); + + Map> expected = new HashMap<>(); + employees.forEach(e -> e.getJobHistory() + .forEach(emp -> { + String employer = emp.getEmployer(); + expected.computeIfAbsent(employer, i -> new ArrayList<>()); + expected.get(employer).add(e.getPerson()); + })); + assertEquals(expected, employersStuffLists); + } + + private static PersonEmployerPair firstEmployerPersonPair(Employee employee) { + final JobHistoryEntry jobHistoryEntry = employee.getJobHistory().stream() + .findFirst() + .get(); + + return new PersonEmployerPair(employee.getPerson(), jobHistoryEntry.getEmployer()); } @Test public void indexByFirstEmployer() { - Map> employeesIndex = null;// TODO - throw new UnsupportedOperationException(); + Map> employeesIndex = getEmployees().stream() + .flatMap(e -> e.getJobHistory() + .stream() + .map(j -> new PersonEmployerPair(e.getPerson(), j.getEmployer())) + .limit(1)) + .collect(groupingBy( + PersonEmployerPair::getEmployer, + mapping(PersonEmployerPair::getPerson, toList())) + ); + + Map> expected = getEmployees().stream() + .map(StreamsExercise2::firstEmployerPersonPair) + .collect(groupingBy(PersonEmployerPair::getEmployer, + mapping(PersonEmployerPair::getPerson, toList()))); + + assertEquals(expected, employeesIndex); } @Test public void greatestExperiencePerEmployer() { - Map employeesIndex = null;// TODO + Map employeesIndex = getEmployees().stream() + .flatMap(e -> e.getJobHistory().stream() + .map(emp -> new PersonEmployerDuration(e.getPerson(), emp.getEmployer(), emp.getDuration()))) + .collect(groupingBy( + PersonEmployerDuration::getEmployer, + collectingAndThen( + maxBy(comparing(PersonEmployerDuration::getDuration)), p -> p.get().getPerson())) + ); assertEquals(new Person("John", "White", 28), employeesIndex.get("epam")); } From c004345bd7b546b606e9d0b7b5af8140ac7c0374 Mon Sep 17 00:00:00 2001 From: Aleksandra Pankratova Date: Sat, 22 Apr 2017 21:18:03 +0300 Subject: [PATCH 2/3] - part2 is done --- .../part2/exercise/CollectorsExercise1.java | 75 +++++++++++--- .../part2/exercise/CollectorsExercise2.java | 98 ++++++++++--------- 2 files changed, 113 insertions(+), 60 deletions(-) diff --git a/src/test/java/part2/exercise/CollectorsExercise1.java b/src/test/java/part2/exercise/CollectorsExercise1.java index 46b6765..2ebfe84 100755 --- a/src/test/java/part2/exercise/CollectorsExercise1.java +++ b/src/test/java/part2/exercise/CollectorsExercise1.java @@ -16,15 +16,18 @@ import java.util.stream.IntStream; import java.util.stream.Stream; -import static java.util.stream.Collectors.toList; +import static java.util.stream.Collectors.*; +import static org.junit.Assert.assertEquals; public class CollectorsExercise1 { @Test public void getTheCoolestOne() { final Map coolestByPosition = getCoolestByPosition(getEmployees()); + final Map coolestByPosition1 = getCoolestByPosition(getEmployees()); coolestByPosition.forEach((position, person) -> System.out.println(position + " -> " + person)); + assertEquals(coolestByPosition, coolestByPosition1); } private static class PersonPositionDuration { @@ -53,30 +56,75 @@ public int getDuration() { // With the longest duration on single job private Map getCoolestByPosition(List employees) { - // First option - // Collectors.maxBy - // Collectors.collectingAndThen - // Collectors.groupingBy - - // Second option - // Collectors.toMap - // iterate twice: stream...collect(...).stream()... - // TODO - throw new UnsupportedOperationException(); + return getPersonPositionDurationStream(employees).collect( + groupingBy(PersonPositionDuration::getPosition, + collectingAndThen(maxBy(Comparator.comparing(PersonPositionDuration::getDuration)), + personPositionDuration -> personPositionDuration.get().getPerson()))); + } + + private Stream getPersonPositionDurationStream(List employees) { + return employees + .stream() + .flatMap(e -> e.getJobHistory().stream() + .map(j -> new PersonPositionDuration(e.getPerson(), j.getPosition(), j.getDuration()))); } @Test public void getTheCoolestOne2() { final Map coolestByPosition = getCoolestByPosition2(getEmployees()); + final Map coolestByPosition1 = getCoolestByPosition2(getEmployees()); coolestByPosition.forEach((position, person) -> System.out.println(position + " -> " + person)); + assertEquals(coolestByPosition, coolestByPosition1); } // With the longest sum duration on this position // { John Doe, [{dev, google, 4}, {dev, epam, 4}] } предпочтительнее, чем { A B, [{dev, google, 6}, {QA, epam, 100}]} private Map getCoolestByPosition2(List employees) { - // TODO - throw new UnsupportedOperationException(); + return getPersonPositionDurationStream(employees).collect( + new Collector, Map>() { + @Override + public Supplier> supplier() { + return HashMap::new; + } + + @Override + public BiConsumer, PersonPositionDuration> accumulator() { + return (map, ppd) -> { + map.putIfAbsent(ppd.getPosition(), ppd); + PersonPositionDuration currentPpd = map.get(ppd.getPosition()); + if (ppd.getDuration() > currentPpd.getDuration()) { + map.put(ppd.getPosition(), ppd); + } + }; + } + + @Override + public BinaryOperator> combiner() { + return (m1, m2) -> { + m2.forEach((k, v) -> m1.merge(k, v, (v1, v2) -> v1.getDuration() > v2.getDuration() ? v1 : v2)); + return m1; + }; + } + + @Override + public Function, Map> finisher() { + return map -> map.entrySet() + .stream() + .collect( + toMap( + Map.Entry::getKey, + item -> item.getValue().getPerson() + ) + ); + } + + @Override + public Set characteristics() { + return Collections.unmodifiableSet(EnumSet.of(Characteristics.UNORDERED)); + } + } + ); } private List getEmployees() { @@ -155,5 +203,4 @@ private List getEmployees() { )) ); } - } diff --git a/src/test/java/part2/exercise/CollectorsExercise2.java b/src/test/java/part2/exercise/CollectorsExercise2.java index 00fda00..37c65fa 100755 --- a/src/test/java/part2/exercise/CollectorsExercise2.java +++ b/src/test/java/part2/exercise/CollectorsExercise2.java @@ -16,7 +16,10 @@ import java.util.stream.IntStream; import java.util.stream.Stream; +import static java.util.stream.Collectors.groupingBy; import static java.util.stream.Collectors.toList; +import static java.util.stream.Collectors.toMap; +import static org.junit.Assert.assertEquals; public class CollectorsExercise2 { @@ -176,24 +179,59 @@ public void collectKeyValueMap() { // final Map> keyValuesMap1 = valueMap1.entrySet().stream()... // В 1 проход в 2 Map с использованием MapPair и mapMerger + + Map keyMap1 = pairs.stream() + .collect( + toMap(p -> p.getKey().getId(), + Pair::getKey, + (v1, v2) -> v1 + ) + ); + + Map> valuesMap1 = pairs.stream() + .collect( + groupingBy(p -> p.getValue().getKeyId(), + Collectors.mapping(Pair::getValue, toList()) + ) + ); + + Map> keyValuesMap1 = valuesMap1.entrySet().stream() + .collect( + toMap( + p -> keyMap1.get(p.getKey()), + Map.Entry::getValue + ) + ); + final MapPair res2 = pairs.stream() .collect(new Collector() { @Override public Supplier supplier() { - // TODO - throw new UnsupportedOperationException(); + return MapPair::new; } @Override public BiConsumer accumulator() { - // TODO add key and value to maps - throw new UnsupportedOperationException(); + return (mp, p) -> { + mp.getKeyById().computeIfAbsent(p.getKey().getId(), q -> p.getKey()); + mp.getValueById().computeIfAbsent(p.getValue().getKeyId(), q -> new ArrayList<>()) + .add(p.getValue()); + }; } @Override public BinaryOperator combiner() { - // TODO use mapMerger - throw new UnsupportedOperationException(); + return (mp1, mp2) -> { + BinaryOperator> biOpKey = mapMerger((v1, v2) -> v1); + Map mapKey = biOpKey.apply(mp1.getKeyById(), mp2.getKeyById()); + + BinaryOperator>> biOpValue = mapMerger((v1, v2) -> { + v1.addAll(v2); + return v1; + }); + Map> mapValue = biOpValue.apply(mp1.getValueById(), mp2.getValueById()); + return new MapPair(mapKey, mapValue); + }; } @Override @@ -212,46 +250,14 @@ public Set characteristics() { final Map keyMap2 = res2.getKeyById(); final Map> valuesMap2 = res2.getValueById(); - // final Map> keyValuesMap2 = valueMap2.entrySet().stream()... - - // Получение результата сразу: - - final SubResult res3 = pairs.stream() - .collect(new Collector() { - @Override - public Supplier supplier() { - // TODO - throw new UnsupportedOperationException(); - } - - @Override - public BiConsumer accumulator() { - // TODO add key to map, then check value.keyId and add it to one of maps - throw new UnsupportedOperationException(); - } - - @Override - public BinaryOperator combiner() { - // TODO use mapMerger, then check all valuesWithoutKeys - throw new UnsupportedOperationException(); - } - - @Override - public Function finisher() { - // TODO use mapMerger, then check all valuesWithoutKeys - throw new UnsupportedOperationException(); - } - - @Override - public Set characteristics() { - return Collections.unmodifiableSet(EnumSet.of( - Characteristics.UNORDERED)); - } - }); - - final Map> keyValuesMap3 = res3.getSubResult(); - - // compare results + final Map> keyValuesMap2 = valuesMap2.entrySet().stream() + .collect( + toMap( + p -> keyMap2.get(p.getKey()), + Map.Entry::getValue + ) + ); + assertEquals(keyValuesMap1, keyValuesMap2); } } From 08be6f346fe811e845861726a4628e79c35c2ee7 Mon Sep 17 00:00:00 2001 From: Aleksandra Pankratova Date: Sat, 22 Apr 2017 22:26:31 +0300 Subject: [PATCH 3/3] - part3 is done --- .../part2/exercise/CollectorsExercise2.java | 2 +- .../part3/exercise/CollectorCombination.java | 52 +++++++++---- .../part3/exercise/lambda/LambdaExercise.java | 36 +++++---- .../exercise/stream/StreamsExercise.java | 78 +++++++++++++++---- 4 files changed, 119 insertions(+), 49 deletions(-) diff --git a/src/test/java/part2/exercise/CollectorsExercise2.java b/src/test/java/part2/exercise/CollectorsExercise2.java index 37c65fa..a72ce66 100755 --- a/src/test/java/part2/exercise/CollectorsExercise2.java +++ b/src/test/java/part2/exercise/CollectorsExercise2.java @@ -134,7 +134,7 @@ public Map> getKnownKeys() { } } - private static class MapPair { + public static class MapPair { private final Map keyById; private final Map> valueById; diff --git a/src/test/java/part3/exercise/CollectorCombination.java b/src/test/java/part3/exercise/CollectorCombination.java index 15a42d5..65c9dc5 100755 --- a/src/test/java/part3/exercise/CollectorCombination.java +++ b/src/test/java/part3/exercise/CollectorCombination.java @@ -1,13 +1,17 @@ package part3.exercise; +import data.Person; import org.junit.Test; +import part2.exercise.CollectorsExercise1; import part2.exercise.CollectorsExercise2; import part2.exercise.CollectorsExercise2.Key; import part2.exercise.CollectorsExercise2.Value; -import java.util.List; -import java.util.Map; +import java.util.*; +import java.util.function.BiConsumer; +import java.util.function.BinaryOperator; import java.util.function.Function; +import java.util.function.Supplier; import java.util.stream.Collector; import static java.util.stream.Collectors.*; @@ -34,18 +38,41 @@ public B getB() { private static Collector, Pair> paired(Collector c1, Collector c2) { - // TODO - throw new UnsupportedOperationException(); + return new Collector, Pair>() { + + @Override + public Supplier> supplier() { + return () -> new Pair<>(c1.supplier().get(), c2.supplier().get()); + } + + @Override + public BiConsumer, T> accumulator() { + return (pair, t) -> { + c1.accumulator().accept(pair.getA(), t); + c2.accumulator().accept(pair.getB(), t); + }; + } + + @Override + public BinaryOperator> combiner() { + return (pair1, pair2) -> + new Pair<>(c1.combiner().apply(pair1.getA(), pair2.getA()), c2.combiner().apply(pair1.getB(), pair2.getB())); + } + + @Override + public Function, Pair> finisher() { + return pair -> new Pair<>(c1.finisher().apply(pair.getA()), c2.finisher().apply(pair.getB())); + } + + @Override + public Set characteristics() { + return Collections.emptySet(); + } + }; } @Test public void collectKeyValueMap() { - // TODO see CollectorsExercise1::collectKeyValueMap - // В 1 проход в 2 Map с использованием MapPair и mapMerger - // final MapPair res2 = pairs.stream() - // .collect(new Collector() { - - // Перепишите решение в слещующем виде: final List pairs = CollectorsExercise2.generatePairs(10, 100); final Pair, Map>> res2 = pairs.stream() @@ -55,10 +82,5 @@ public void collectKeyValueMap() { mapping(CollectorsExercise2.Pair::getValue, groupingBy(Value::getKeyId)) ) ); - - - // TODO tests - throw new UnsupportedOperationException(); } - } diff --git a/src/test/java/part3/exercise/lambda/LambdaExercise.java b/src/test/java/part3/exercise/lambda/LambdaExercise.java index 752e1f2..c8c818f 100755 --- a/src/test/java/part3/exercise/lambda/LambdaExercise.java +++ b/src/test/java/part3/exercise/lambda/LambdaExercise.java @@ -15,19 +15,22 @@ public class LambdaExercise { public void supply() { final Person person = new Person("John", "Galt", 30); - final Supplier getPerson = null; // TODO return person from Supplier + final Supplier getPerson = () -> person; // return person from Supplier assertEquals(person, getPerson.get()); } @Test public void function() { - final Function getPersonName1 = null; // TODO get the name of person using expression lambda + final Function getPersonName1 = person -> person.getFirstName(); // get the name of person using expression lambda - final Function getPersonName2 = null; // TODO get the name of person using method reference + final Function getPersonName2 = Person::getFirstName; // get the name of person using method reference - // TODO get the name of person and log it to System.out using statement lambda: {} - final Function getPersonNameAndLogIt = null; + // get the name of person and log it to System.out using statement lambda: {} + final Function getPersonNameAndLogIt = person -> { + System.out.println(person.getFirstName()); + return person.getFirstName(); + }; final Person person = new Person("John", "Galt", 30); @@ -38,19 +41,19 @@ public void function() { @Test public void combineFunctions() { - final Function getPersonName = null; // TODO get the name of person + final Function getPersonName = Person::getFirstName; // get the name of person assertEquals("John", getPersonName.apply(new Person("John", "Galt", 30))); - final Function getStringLength = null; // TODO get string length + final Function getStringLength = String::length; // get string length assertEquals(Integer.valueOf(3), getStringLength.apply("ABC")); - // TODO get person name length using getPersonName and getStringLength without andThen - final Function getPersonNameLength1 = null; + // get person name length using getPersonName and getStringLength without andThen + final Function getPersonNameLength1 = person -> getStringLength.apply(getPersonName.apply(person)); - // TODO get person name length using getPersonName and getStringLength with andThen - final Function getPersonNameLength2 = null; + // get person name length using getPersonName and getStringLength with andThen + final Function getPersonNameLength2 = getPersonName.andThen(getStringLength); final Person person = new Person("John", "Galt", 30); @@ -68,22 +71,21 @@ private Person createPerson(PersonFactory pf) { // ((T -> R), (R -> boolean)) -> (T -> boolean) private Predicate combine(Function f, Predicate p) { - // TODO - throw new UnsupportedOperationException(); + return (T t) -> p.test(f.apply(t)); } @Test public void methodReference() { - // TODO use only method reverences here. - final Person person = createPerson(null); // TODO + // use only method reverences here. + final Person person = createPerson(Person::new); assertEquals(new Person("John", "Galt", 66), person); - final Function getPersonName = null; // TODO + final Function getPersonName = Person::getFirstName; assertEquals("John", getPersonName.apply(person)); - final Predicate isJohnString = null; // TODO using method reference check that "John" equals string parameter + final Predicate isJohnString = "John"::equals; // using method reference check that "John" equals string parameter final Predicate isJohnPerson = combine(getPersonName, isJohnString); diff --git a/src/test/java/part3/exercise/stream/StreamsExercise.java b/src/test/java/part3/exercise/stream/StreamsExercise.java index 4e2d54b..32ea4e6 100755 --- a/src/test/java/part3/exercise/stream/StreamsExercise.java +++ b/src/test/java/part3/exercise/stream/StreamsExercise.java @@ -10,7 +10,14 @@ import java.util.Collections; import java.util.List; import java.util.Map; - +import java.util.function.BinaryOperator; +import java.util.function.Function; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static java.util.Comparator.comparing; +import static java.util.Comparator.comparingInt; +import static java.util.stream.Collectors.*; import static org.junit.Assert.assertEquals; public class StreamsExercise { @@ -19,7 +26,9 @@ public class StreamsExercise { public void getAllJobHistoryEntries() { final List employees = getEmployees(); - final List jobHistoryEntries = null; // TODO + final List jobHistoryEntries = employees.stream() + .flatMap(e -> e.getJobHistory().stream()) + .collect(toList()); assertEquals(22, jobHistoryEntries.size()); } @@ -29,12 +38,15 @@ public void getSumDuration() { // sum all durations for all persons final List employees = getEmployees(); - final int sumDurations = 0; // TODO + final int sumDurations = employees.stream() + .flatMap(e -> e.getJobHistory().stream()) + .mapToInt(JobHistoryEntry::getDuration) + .sum(); assertEquals(72, sumDurations); } - private static class PersonEmployer{ + private static class PersonEmployer { private final Person person; private final String employer; @@ -64,7 +76,10 @@ public String toString() { public void indexPersonsByEmployer1() { final List employees = getEmployees(); - final Map> index = null; // TODO + final Map> index = employees.stream() + .flatMap(e -> e.getJobHistory().stream() + .map(emp -> new PersonEmployer(e.getPerson(), emp.getEmployer()))) + .collect(groupingBy(PersonEmployer::getEmployer)); assertEquals(11, index.get("epam").size()); } @@ -73,7 +88,12 @@ public void indexPersonsByEmployer1() { public void indexPersonsByEmployer2() { final List employees = getEmployees(); - final Map> index = null; // TODO + final Map> index = employees.stream() + .flatMap(e -> e.getJobHistory().stream() + .map(emp -> new PersonEmployer(e.getPerson(), emp.getEmployer()))) + .collect(groupingBy( + PersonEmployer::getEmployer, + mapping(PersonEmployer::getPerson, toList()))); assertEquals(11, index.get("epam").size()); } @@ -105,8 +125,10 @@ public String toString() { } private PersonDuration sumAllPersonDurations(Employee e) { - // TODO - throw new UnsupportedOperationException(); + int duration = e.getJobHistory().stream() + .mapToInt(JobHistoryEntry::getDuration) + .sum(); + return new PersonDuration(e.getPerson(), duration); } @Test @@ -114,7 +136,10 @@ public void getSumPersonDuration() { // sum all durations for each person final List employees = getEmployees(); - final Map personDuration = null; // TODO use sumAllPersonDurations + final Map personDuration = employees.stream() + .map(this::sumAllPersonDurations) + .collect(toMap(PersonDuration::getPerson, + PersonDuration::getDuration)); assertEquals(Integer.valueOf(8), personDuration.get(new Person("John", "Doe", 24))); } @@ -138,15 +163,19 @@ public Map getDurationByPositionIndex() { } private static PersonPositionIndex getPersonPositionIndex(Employee e) { - // TODO - throw new UnsupportedOperationException(); + Map index = e.getJobHistory().stream() + .collect(groupingBy(JobHistoryEntry::getPosition, + summingInt(JobHistoryEntry::getDuration))); + return new PersonPositionIndex(e.getPerson(), index); } @Test public void getSumDurationsForPersonByPosition() { final List employees = getEmployees(); - final List personIndexes = null; // TODO use getPersonPositionIndex + final List personIndexes = employees.stream() + .map(StreamsExercise::getPersonPositionIndex) + .collect(toList()); assertEquals(1, personIndexes.get(3).getDurationByPositionIndex().size()); } @@ -179,7 +208,11 @@ public int getDuration() { public void getDurationsForEachPersonByPosition() { final List employees = getEmployees(); - final List personPositionDurations = null; // TODO + final List personPositionDurations = employees.stream() + .map(StreamsExercise::getPersonPositionIndex) + .flatMap(e -> e.getDurationByPositionIndex().entrySet().stream() + .map(p -> new PersonPositionDuration(e.getPerson(), p.getKey(), p.getValue()))) + .collect(toList()); assertEquals(17, personPositionDurations.size()); @@ -190,7 +223,14 @@ public void getCoolestPersonByPosition1() { // Get person with max duration on given position final List employees = getEmployees(); - final Map coolestPersonByPosition = null;// TODO + final Map coolestPersonByPosition = employees.stream() + .map(StreamsExercise::getPersonPositionIndex) + .flatMap(e -> e.getDurationByPositionIndex().entrySet().stream() + .map(p -> new PersonPositionDuration(e.getPerson(), p.getKey(), p.getValue()))) + .collect(toMap( + PersonPositionDuration::getPosition, + Function.identity(), + BinaryOperator.maxBy(comparingInt(PersonPositionDuration::getDuration)))); assertEquals(new Person("John", "White", 22), coolestPersonByPosition.get("QA").getPerson()); @@ -201,8 +241,14 @@ public void getCoolestPersonByPosition2() { // Get person with max duration on given position final List employees = getEmployees(); - final Map coolestPersonByPosition = null; // TODO - + final Map coolestPersonByPosition = employees.stream() + .map(StreamsExercise::getPersonPositionIndex) + .flatMap(e -> e.getDurationByPositionIndex().entrySet().stream() + .map(p -> new PersonPositionDuration(e.getPerson(), p.getKey(), p.getValue()))) + .collect(groupingBy(PersonPositionDuration::getPosition, + collectingAndThen(maxBy(comparing(PersonPositionDuration::getDuration)), + k -> k.get().getPerson() + ))); assertEquals(new Person("John", "White", 22), coolestPersonByPosition.get("QA")); }