diff --git a/src/test/java/lambda/part1/exercise/Lambdas01Exercise.java b/src/test/java/lambda/part1/exercise/Lambdas01Exercise.java index b8656b7..dcb3f30 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,12 @@ public 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), @@ -41,7 +48,15 @@ public void findFirstWithAge30() { Person person = null; // TODO use FluentIterable + person = FluentIterable.from(persons) + .firstMatch(new Predicate() { + @Override + public boolean apply(Person p) { + return p.getAge() == 30; + } + }).get(); assertEquals(person, 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..9601049 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(p -> p.getAge())); assertArrayEquals(persons, new Person[]{ new Person("name 3", "lastName 3", 20), @@ -35,10 +38,8 @@ public void findFirstWithAge30() { new Person("name 2", "lastName 1", 30) ); - Person person = null; - - // TODO use FluentIterable + Person person = FluentIterable.from(persons).firstMatch(p -> p.getAge() == 30).get(); assertEquals(person, new Person("name 1", "lastName 2", 30)); } -} +} \ No newline at end of file diff --git a/src/test/java/lambda/part1/exercise/Lambdas03Exercise.java b/src/test/java/lambda/part1/exercise/Lambdas03Exercise.java index 59d3972..69b5fe6 100644 --- a/src/test/java/lambda/part1/exercise/Lambdas03Exercise.java +++ b/src/test/java/lambda/part1/exercise/Lambdas03Exercise.java @@ -18,21 +18,29 @@ 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 3 * 2; + } + }; assertEquals(prod.prod(3, 2), Integer.valueOf(6)); } @Test public void generic1() { - final GenericProduct prod = null; // Use statement lambda + final GenericProduct prod = + (Integer i1, int i2) -> { + return i1 * i2; + }; assertEquals(prod.prod(3, 2), Integer.valueOf(6)); } @Test public void generic2() { - final GenericProduct prod = null; // Use expression lambda + final GenericProduct prod = (i1, i2) -> i1 * i2; // Use expression lambda assertEquals(prod.prod(3, 2), Integer.valueOf(6)); } @@ -47,7 +55,7 @@ private static String stringProd(String s, int i) { @Test public void strSum() { - final GenericProduct prod = null; // use stringProd; + final GenericProduct prod = Lambdas03Exercise::stringProd; // use stringProd; assertEquals(prod.prod("a", 2), "aa"); } @@ -64,10 +72,9 @@ private String stringSumWithDelimeter(String s, int i) { @Test public void strSum2() { - final GenericProduct prod = null; // use stringSumWithDelimeter; + final GenericProduct prod = this::stringSumWithDelimeter; // use stringSumWithDelimeter; assertEquals(prod.prod("a", 3), "a-a-a"); } - }