diff --git a/src/test/java/lambda/part1/exercise/Lambdas01Exercise.java b/src/test/java/lambda/part1/exercise/Lambdas01Exercise.java index b8656b7..4d51eb5 100644 --- a/src/test/java/lambda/part1/exercise/Lambdas01Exercise.java +++ b/src/test/java/lambda/part1/exercise/Lambdas01Exercise.java @@ -1,5 +1,7 @@ package lambda.part1.exercise; +import com.google.common.base.Predicate; +import com.google.common.collect.FluentIterable; import com.google.common.collect.ImmutableList; import data.Person; import org.junit.Test; @@ -21,7 +23,11 @@ public void sortPersonsByAge() { new Person("name 2", "lastName 1", 30) }; - // TODO use Arrays.sort + Arrays.sort(persons, new Comparator() { + public int compare(Person p1, Person p2) { + return Integer.compare(p1.getAge(), p2.getAge()); + } + }); assertArrayEquals(persons, new Person[]{ new Person("name 3", "lastName 3", 20), @@ -38,10 +44,19 @@ public void findFirstWithAge30() { new Person("name 2", "lastName 1", 30) ); - Person person = null; - - // TODO use FluentIterable + final Person[] person = {null}; + + //noinspection Guava + FluentIterable.from(persons).anyMatch(new Predicate() { + public boolean apply(Person curPerson) { + if (curPerson.getAge() == 30) { + person[0] = curPerson; + return true; + } + return false; + } + }); - assertEquals(person, new Person("name 1", "lastName 2", 30)); + assertEquals(person[0], new Person("name 1", "lastName 2", 30)); } } diff --git a/src/test/java/lambda/part1/exercise/Lambdas02Exercise.java b/src/test/java/lambda/part1/exercise/Lambdas02Exercise.java index da29209..d1c9dce 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.Test; +import java.util.Arrays; +import java.util.Comparator; import java.util.List; import static org.junit.Assert.assertArrayEquals; @@ -18,7 +21,7 @@ public void sortPersonsByAge() { new Person("name 2", "lastName 1", 30) }; - // TODO use Arrays.sort + Arrays.sort(persons, Comparator.comparingInt(Person::getAge)); assertArrayEquals(persons, new Person[]{ new Person("name 3", "lastName 3", 20), @@ -35,10 +38,16 @@ public void findFirstWithAge30() { new Person("name 2", "lastName 1", 30) ); - Person person = null; + final Person[] person = {null}; - // TODO use FluentIterable + FluentIterable.from(persons).anyMatch(curPerson -> { + if (curPerson.getAge() == 30) { + person[0] = curPerson; + return true; + } + return false; + }); - assertEquals(person, new Person("name 1", "lastName 2", 30)); + assertEquals(person[0], new Person("name 1", "lastName 2", 30)); } } diff --git a/src/test/java/lambda/part1/exercise/Lambdas03Exercise.java b/src/test/java/lambda/part1/exercise/Lambdas03Exercise.java index 59d3972..3743bc3 100644 --- a/src/test/java/lambda/part1/exercise/Lambdas03Exercise.java +++ b/src/test/java/lambda/part1/exercise/Lambdas03Exercise.java @@ -18,21 +18,27 @@ default T twice(T t) { @Test public void generic0() { - final GenericProduct prod = null; // Use anonymous class + final GenericProduct prod = new GenericProduct() { + @Override + public Integer prod(Integer a, int i) { + return a*i; + } + }; assertEquals(prod.prod(3, 2), Integer.valueOf(6)); } @Test public void generic1() { - final GenericProduct prod = null; // Use statement lambda - + final GenericProduct prod = (i, j) -> { + return i*j; + }; assertEquals(prod.prod(3, 2), Integer.valueOf(6)); } @Test public void generic2() { - final GenericProduct prod = null; // Use expression lambda + final GenericProduct prod = (i, j) -> i*j; assertEquals(prod.prod(3, 2), Integer.valueOf(6)); } @@ -47,7 +53,7 @@ private static String stringProd(String s, int i) { @Test public void strSum() { - final GenericProduct prod = null; // use stringProd; + final GenericProduct prod = Lambdas03Exercise::stringProd; assertEquals(prod.prod("a", 2), "aa"); } @@ -64,7 +70,7 @@ private String stringSumWithDelimeter(String s, int i) { @Test public void strSum2() { - final GenericProduct prod = null; // use stringSumWithDelimeter; + final GenericProduct prod = this::stringSumWithDelimeter; assertEquals(prod.prod("a", 3), "a-a-a"); }