From 6f7f885e86f1c70a3c7599a15575c27f4b921b4a Mon Sep 17 00:00:00 2001 From: Michael-Georg Date: Fri, 31 Mar 2017 11:12:21 +0300 Subject: [PATCH 1/2] Lambdas01Exercise DONE --- .../part1/exercise/Lambdas01Exercise.java | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/test/java/lambda/part1/exercise/Lambdas01Exercise.java b/src/test/java/lambda/part1/exercise/Lambdas01Exercise.java index 14cfc0d..1323091 100644 --- a/src/test/java/lambda/part1/exercise/Lambdas01Exercise.java +++ b/src/test/java/lambda/part1/exercise/Lambdas01Exercise.java @@ -1,9 +1,14 @@ package lambda.part1.exercise; +import com.google.common.base.Optional; +import com.google.common.base.Predicate; +import com.google.common.collect.FluentIterable; import com.google.common.collect.ImmutableList; import data.Person; import org.junit.jupiter.api.Test; +import java.util.Arrays; +import java.util.Comparator; import java.util.List; import static org.junit.jupiter.api.Assertions.assertArrayEquals; @@ -19,7 +24,12 @@ void sortPersonsByAge() { new Person("name 2", "lastName 1", 30) }; - // TODO use Arrays.sort + Arrays.sort(persons, new Comparator() { + @Override + public int compare(Person o1, Person o2) { + return Integer.compare(o1.getAge(),o2.getAge()); + } + }); assertArrayEquals(persons, new Person[]{ new Person("name 3", "lastName 3", 20), @@ -36,10 +46,17 @@ void findFirstWithAge30() { new Person("name 2", "lastName 1", 30) ); - Person person = null; // TODO use FluentIterable - + Optional personOptional =FluentIterable.from(persons) + .firstMatch(new Predicate() { + @Override + public boolean apply(Person input) { + return input.getAge() == 30; + } + }); + + Person person = personOptional.isPresent() ? personOptional.get() : null; assertEquals(person, new Person("name 1", "lastName 2", 30)); } } From 7bd86b0b6e4db7282b01fba1002641ef68eda8a9 Mon Sep 17 00:00:00 2001 From: Michael-Georg Date: Mon, 3 Apr 2017 20:54:48 +0300 Subject: [PATCH 2/2] Lambdas02Exercise done --- src/test/java/lambda/part1/exercise/Lambdas02Exercise.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/test/java/lambda/part1/exercise/Lambdas02Exercise.java b/src/test/java/lambda/part1/exercise/Lambdas02Exercise.java index 8f47d34..4bc677f 100644 --- a/src/test/java/lambda/part1/exercise/Lambdas02Exercise.java +++ b/src/test/java/lambda/part1/exercise/Lambdas02Exercise.java @@ -1,9 +1,12 @@ package lambda.part1.exercise; +import com.google.common.collect.FluentIterable; import com.google.common.collect.ImmutableList; import data.Person; import org.junit.jupiter.api.Test; +import java.util.Arrays; +import java.util.Comparator; import java.util.List; import static org.junit.jupiter.api.Assertions.assertArrayEquals; @@ -19,6 +22,7 @@ void sortPersonsByAge() { }; // TODO use Arrays.sort + Arrays.sort(persons, Comparator.comparingInt(Person::getAge)); assertArrayEquals(persons, new Person[]{ new Person("name 3", "lastName 3", 20), @@ -35,9 +39,10 @@ void findFirstWithAge30() { new Person("name 2", "lastName 1", 30) ); - Person person = null; // TODO use FluentIterable + Person person = FluentIterable.from(persons) + .firstMatch(input->input.getAge() == 30).orNull(); assertEquals(person, new Person("name 1", "lastName 2", 30)); }