diff --git a/src/test/java/part3/exercise/CollectorCombination.java b/src/test/java/part3/exercise/CollectorCombination.java index 15a42d5..9e6e39e 100755 --- a/src/test/java/part3/exercise/CollectorCombination.java +++ b/src/test/java/part3/exercise/CollectorCombination.java @@ -5,12 +5,15 @@ 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.*; +import static org.junit.Assert.assertEquals; public class CollectorCombination { @@ -32,10 +35,50 @@ public B getB() { } } - private static Collector, Pair> paired(Collector c1, - Collector c2) { - // TODO - throw new UnsupportedOperationException(); + private static Collector, Pair> paired( + Collector c1, + Collector c2 + ) { + 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 (p1, p2) -> + new Pair( + c1.combiner().apply(p1.getA(), p2.getA()), + c2.combiner().apply(p1.getB(), p2.getB()) + ); + } + + @Override + public Function, Pair> finisher() { + return (pair) -> new Pair( + c1.finisher().apply(pair.getA()), + c2.finisher().apply(pair.getB()) + ); + } + + @Override + public Set characteristics() { + Set characteristics = new HashSet<>(c1.characteristics()); + + characteristics.retainAll(c2.characteristics()); + + return characteristics; + } + }; } @Test @@ -57,8 +100,15 @@ public void collectKeyValueMap() { ); - // TODO tests - throw new UnsupportedOperationException(); + Map collect = pairs.stream() + .collect(mapping(CollectorsExercise2.Pair::getKey, toMap(Key::getId, Function.identity(), (x, y) -> x))); + + Map> collect1 = pairs.stream() + .collect(mapping(CollectorsExercise2.Pair::getValue, groupingBy(Value::getKeyId))); + + assertEquals(res2.getA(), collect); + assertEquals(res2.getB(), collect1); + } } diff --git a/src/test/java/part3/exercise/lambda/LambdaExercise.java b/src/test/java/part3/exercise/lambda/LambdaExercise.java index 752e1f2..7d4530b 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; 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(); - final Function getPersonName2 = null; // TODO get the name of person using method reference + final Function getPersonName2 = Person::getFirstName; - // TODO get the name of person and log it to System.out using statement lambda: {} - final Function getPersonNameAndLogIt = null; + final Function getPersonNameAndLogIt = person -> { + String firstName = person.getFirstName(); + System.out.println(firstName); + return firstName; + }; final Person person = new Person("John", "Galt", 30); @@ -38,19 +41,17 @@ public void function() { @Test public void combineFunctions() { - final Function getPersonName = null; // TODO get the name of person + final Function getPersonName = Person::getFirstName; assertEquals("John", getPersonName.apply(new Person("John", "Galt", 30))); - final Function getStringLength = null; // TODO get string length + final Function getStringLength = String::length; assertEquals(Integer.valueOf(3), getStringLength.apply("ABC")); - // TODO get person name length using getPersonName and getStringLength without andThen - final Function getPersonNameLength1 = null; + final Function getPersonNameLength1 = person -> getStringLength.apply(getPersonName.apply(person)); - // TODO get person name length using getPersonName and getStringLength with andThen - final Function getPersonNameLength2 = null; + final Function getPersonNameLength2 = getPersonName.andThen(getStringLength); final Person person = new Person("John", "Galt", 30); @@ -68,22 +69,20 @@ private Person createPerson(PersonFactory pf) { // ((T -> R), (R -> boolean)) -> (T -> boolean) private Predicate combine(Function f, Predicate p) { - // TODO - throw new UnsupportedOperationException(); + return t -> p.test(f.apply(t)); } @Test public void methodReference() { - // TODO use only method reverences here. - final Person person = createPerson(null); // TODO + 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; 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..4abefcd 100755 --- a/src/test/java/part3/exercise/stream/StreamsExercise.java +++ b/src/test/java/part3/exercise/stream/StreamsExercise.java @@ -6,10 +6,10 @@ import org.apache.commons.lang3.builder.ToStringBuilder; 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.stream.Collectors; +import java.util.stream.Stream; import static org.junit.Assert.assertEquals; @@ -19,7 +19,9 @@ public class StreamsExercise { public void getAllJobHistoryEntries() { final List employees = getEmployees(); - final List jobHistoryEntries = null; // TODO + final List jobHistoryEntries = employees.stream() + .flatMap(employee -> employee.getJobHistory().stream()) + .collect(Collectors.toList()); assertEquals(22, jobHistoryEntries.size()); } @@ -29,12 +31,19 @@ public void getSumDuration() { // sum all durations for all persons final List employees = getEmployees(); - final int sumDurations = 0; // TODO + final int sumDurations = employees.stream() + .flatMap(this::toJobHistoryEntry) + .mapToInt(JobHistoryEntry::getDuration) + .sum(); - assertEquals(72, sumDurations); + assertEquals(74, sumDurations); } - private static class PersonEmployer{ + private Stream toJobHistoryEntry(Employee employee) { + return employee.getJobHistory().stream(); + } + + private static class PersonEmployer { private final Person person; private final String employer; @@ -64,16 +73,35 @@ public String toString() { public void indexPersonsByEmployer1() { final List employees = getEmployees(); - final Map> index = null; // TODO + final Map> index = employees.stream() + .flatMap(this::toPersonEmployer) + .collect( + Collectors.groupingBy( + PersonEmployer::getEmployer, + Collectors.toList() + ) + ); assertEquals(11, index.get("epam").size()); } + private Stream toPersonEmployer(Employee employee) { + return employee.getJobHistory().stream() + .map(jobHistoryEntry -> new PersonEmployer(employee.getPerson(), jobHistoryEntry.getEmployer())); + } + @Test public void indexPersonsByEmployer2() { final List employees = getEmployees(); - final Map> index = null; // TODO + final Map> index = employees.stream() + .flatMap(this::toPersonEmployer) + .collect( + Collectors.groupingBy( + PersonEmployer::getEmployer, + Collectors.mapping(PersonEmployer::getPerson, Collectors.toList()) + ) + ); assertEquals(11, index.get("epam").size()); } @@ -105,16 +133,24 @@ public String toString() { } private PersonDuration sumAllPersonDurations(Employee e) { - // TODO - throw new UnsupportedOperationException(); + return new PersonDuration( + e.getPerson(), + e.getJobHistory().stream().mapToInt(JobHistoryEntry::getDuration).sum() + ); } @Test 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( + Collectors.toMap( + PersonDuration::getPerson, + PersonDuration::getDuration + ) + ); assertEquals(Integer.valueOf(8), personDuration.get(new Person("John", "Doe", 24))); } @@ -138,15 +174,23 @@ public Map getDurationByPositionIndex() { } private static PersonPositionIndex getPersonPositionIndex(Employee e) { - // TODO - throw new UnsupportedOperationException(); + return new PersonPositionIndex( + e.getPerson(), + e.getJobHistory().stream().collect(Collectors.toMap( + JobHistoryEntry::getPosition, + JobHistoryEntry::getDuration, + (a1, a2) -> a1 + a2 + )) + ); } @Test public void getSumDurationsForPersonByPosition() { final List employees = getEmployees(); - final List personIndexes = null; // TODO use getPersonPositionIndex + final List personIndexes = employees.stream() + .map(StreamsExercise::getPersonPositionIndex) + .collect(Collectors.toList()); assertEquals(1, personIndexes.get(3).getDurationByPositionIndex().size()); } @@ -179,18 +223,42 @@ public int getDuration() { public void getDurationsForEachPersonByPosition() { final List employees = getEmployees(); - final List personPositionDurations = null; // TODO - + final List personPositionDurations = employees.stream() + .map(StreamsExercise::getPersonPositionIndex) + .flatMap( + personPositionIndex -> personPositionIndex.getDurationByPositionIndex().entrySet() + .stream() + .map(pair -> new PersonPositionDuration(personPositionIndex.getPerson(), pair.getKey(), pair.getValue())) + ) + .collect(Collectors.toList()); assertEquals(17, personPositionDurations.size()); } + private Stream toPersonPositionDuration(Employee employee) { + return employee.getJobHistory().stream() + .map(jobHistoryEntry -> new PersonPositionDuration(employee.getPerson(), jobHistoryEntry.getPosition(), jobHistoryEntry.getDuration())); + } + @Test 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( + personPositionIndex -> personPositionIndex.getDurationByPositionIndex().entrySet() + .stream() + .map(pair -> new PersonPositionDuration(personPositionIndex.getPerson(), pair.getKey(), pair.getValue())) + ) + .collect( + Collectors.toMap( + PersonPositionDuration::getPosition, + Function.identity(), + (p1, p2) -> p1.getDuration() > p2.getDuration() ? p1 : p2 + ) + ); assertEquals(new Person("John", "White", 22), coolestPersonByPosition.get("QA").getPerson()); @@ -201,7 +269,23 @@ 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( + personPositionIndex -> personPositionIndex.getDurationByPositionIndex().entrySet() + .stream() + .map(pair -> new PersonPositionDuration(personPositionIndex.getPerson(), pair.getKey(), pair.getValue())) + ) + .collect( + Collectors.groupingBy( + PersonPositionDuration::getPosition, + Collectors.collectingAndThen( + Collectors.maxBy(Comparator.comparing(PersonPositionDuration::getDuration)), + optional -> optional.get().getPerson() + ) + ) + ); assertEquals(new Person("John", "White", 22), coolestPersonByPosition.get("QA")); @@ -222,7 +306,7 @@ private List getEmployees() { new Employee( new Person("John", "White", 22), Collections.singletonList( - new JobHistoryEntry(6, "QA", "epam") + new JobHistoryEntry(8, "QA", "epam") )), new Employee( new Person("John", "Galt", 23),