From b36dda5de0c7ff0668c496abb6b1a1716977a37d Mon Sep 17 00:00:00 2001 From: andrei Date: Mon, 3 Jul 2017 22:05:15 +0300 Subject: [PATCH 1/3] part1 was done --- .../part1/exercise/Lambdas01Exercise.java | 25 +++++++++++++++---- .../part1/exercise/Lambdas02Exercise.java | 16 +++++++++--- .../part1/exercise/Lambdas03Exercise.java | 18 ++++++++----- 3 files changed, 44 insertions(+), 15 deletions(-) diff --git a/src/test/java/lambda/part1/exercise/Lambdas01Exercise.java b/src/test/java/lambda/part1/exercise/Lambdas01Exercise.java index b8656b7..5631db4 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 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..d1cf0d2 100644 --- a/src/test/java/lambda/part1/exercise/Lambdas02Exercise.java +++ b/src/test/java/lambda/part1/exercise/Lambdas02Exercise.java @@ -1,9 +1,11 @@ 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.List; import static org.junit.Assert.assertArrayEquals; @@ -18,7 +20,7 @@ public void sortPersonsByAge() { new Person("name 2", "lastName 1", 30) }; - // TODO use Arrays.sort + Arrays.sort(persons, (p1, p2) -> p1.getAge() - p2.getAge()); assertArrayEquals(persons, new Person[]{ new Person("name 3", "lastName 3", 20), @@ -35,10 +37,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"); } From 6ba31f3c7a42f2ebba97aa684b79fa653732f50e Mon Sep 17 00:00:00 2001 From: andrei Date: Mon, 3 Jul 2017 22:05:15 +0300 Subject: [PATCH 2/3] part1 was done --- .../part1/exercise/Lambdas01Exercise.java | 25 +++++++++++++++---- .../part1/exercise/Lambdas02Exercise.java | 16 +++++++++--- .../part1/exercise/Lambdas03Exercise.java | 18 ++++++++----- 3 files changed, 44 insertions(+), 15 deletions(-) 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..4ec2b98 100644 --- a/src/test/java/lambda/part1/exercise/Lambdas02Exercise.java +++ b/src/test/java/lambda/part1/exercise/Lambdas02Exercise.java @@ -1,9 +1,11 @@ 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.List; import static org.junit.Assert.assertArrayEquals; @@ -18,7 +20,7 @@ public void sortPersonsByAge() { new Person("name 2", "lastName 1", 30) }; - // TODO use Arrays.sort + Arrays.sort(persons, (p1, p2) -> Integer.compare(p1.getAge(), p2.getAge())); assertArrayEquals(persons, new Person[]{ new Person("name 3", "lastName 3", 20), @@ -35,10 +37,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"); } From c0658be1a595b6ab8eeae530a7ba0d70106814c4 Mon Sep 17 00:00:00 2001 From: andrei Date: Wed, 5 Jul 2017 21:07:12 +0300 Subject: [PATCH 3/3] Comparator.comparingInt(Person::getAge) was used instead expression lambda --- src/test/java/lambda/part1/exercise/Lambdas02Exercise.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/java/lambda/part1/exercise/Lambdas02Exercise.java b/src/test/java/lambda/part1/exercise/Lambdas02Exercise.java index 4ec2b98..d1c9dce 100644 --- a/src/test/java/lambda/part1/exercise/Lambdas02Exercise.java +++ b/src/test/java/lambda/part1/exercise/Lambdas02Exercise.java @@ -6,6 +6,7 @@ import org.junit.Test; import java.util.Arrays; +import java.util.Comparator; import java.util.List; import static org.junit.Assert.assertArrayEquals; @@ -20,7 +21,7 @@ public void sortPersonsByAge() { new Person("name 2", "lastName 1", 30) }; - Arrays.sort(persons, (p1, p2) -> Integer.compare(p1.getAge(), p2.getAge())); + Arrays.sort(persons, Comparator.comparingInt(Person::getAge)); assertArrayEquals(persons, new Person[]{ new Person("name 3", "lastName 3", 20),