From 47eb3447617f8dcf7c34323bbe4990b2ac9ab094 Mon Sep 17 00:00:00 2001 From: Paulinemereghetti Date: Mon, 27 Mar 2017 16:55:43 +0200 Subject: [PATCH] =?UTF-8?q?commit=20de=20l'exercice=201=20et=202=20non=20t?= =?UTF-8?q?ermin=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Paulinemereghetti --- .../src/test/java/stream/api/Exercise1Test.java | 8 ++++---- .../src/test/java/stream/api/Exercise2Test.java | 14 +++++++------- 2 files changed, 11 insertions(+), 11 deletions(-) mode change 100644 => 100755 stream-api/src/test/java/stream/api/Exercise1Test.java mode change 100644 => 100755 stream-api/src/test/java/stream/api/Exercise2Test.java diff --git a/stream-api/src/test/java/stream/api/Exercise1Test.java b/stream-api/src/test/java/stream/api/Exercise1Test.java old mode 100644 new mode 100755 index 368443e..fb3f204 --- a/stream-api/src/test/java/stream/api/Exercise1Test.java +++ b/stream-api/src/test/java/stream/api/Exercise1Test.java @@ -27,8 +27,8 @@ public void findRichCustomers() { * Create a {@link Stream} from customerList only including customer who has more budget than 10000. * Use lambda expression for Predicate and {@link Stream#filter} for filtering. */ - Predicate richCustomerCondition = null; - Stream richCustomerStream = null; + Predicate richCustomerCondition = customer -> customer.getBudget() > 10000; + Stream richCustomerStream = customerList.stream().filter(richCustomerCondition); assertTrue("Solution for Predicate should be lambda expression", AssertUtil.isLambda(richCustomerCondition)); List richCustomer = richCustomerStream.collect(Collectors.toList()); @@ -45,8 +45,8 @@ public void howOldAreTheCustomers() { * Use method reference(best) or lambda expression(okay) for creating {@link Function} which will * convert {@link Customer} to {@link Integer}, and then apply it by using {@link Stream#map}. */ - Function getAgeFunction = null; - Stream ageStream = null; + Function getAgeFunction = Customer::getAge; + Stream ageStream = customerList.stream().map(getAgeFunction); assertTrue(AssertUtil.isLambda(getAgeFunction)); List richCustomer = ageStream.collect(Collectors.toList()); diff --git a/stream-api/src/test/java/stream/api/Exercise2Test.java b/stream-api/src/test/java/stream/api/Exercise2Test.java old mode 100644 new mode 100755 index a7f8956..d039038 --- a/stream-api/src/test/java/stream/api/Exercise2Test.java +++ b/stream-api/src/test/java/stream/api/Exercise2Test.java @@ -27,7 +27,7 @@ public void sortByAge() { * Create a stream with ascending ordered age values. * Use {@link Stream#sorted} to sort them. */ - Stream sortedAgeStream = null; + Stream sortedAgeStream = customerList.stream().map(Customer::getAge).sorted(); List sortedAgeList = sortedAgeStream.collect(Collectors.toList()); assertThat(sortedAgeList, contains(21, 22, 22, 26, 27, 28, 32, 35, 36, 38)); @@ -40,8 +40,8 @@ public void descSortByAge() { /** * Create a stream with descending ordered age values. */ - Comparator descOrder = null; - Stream sortedAgeStream = null; + Comparator descOrder = ((o1, o2) -> o1 - o2); + Stream sortedAgeStream = customerList.stream().map(Customer::getAge).sorted(descOrder); assertTrue(AssertUtil.isLambda(descOrder)); List sortedAgeList = sortedAgeStream.collect(Collectors.toList()); @@ -55,7 +55,7 @@ public void top3RichCustomer() { /** * Create a stream with top 3 rich customers using {@link Stream#limit} to limit the size of the stream */ - Stream top3RichCustomerStream = null; + Stream top3RichCustomerStream = customerList.stream().sorted((o1, o2) -> o1.getBudget()).map(Customer::getName).limit(3); List top3RichCustomerList = top3RichCustomerStream.collect(Collectors.toList()); assertThat(top3RichCustomerList, contains("Diana", "Andrew", "Chris")); @@ -68,7 +68,7 @@ public void distinctAge() { /** * Create a stream with distinct age values using {@link Stream#distinct} */ - Stream distinctAgeStream = null; + Stream distinctAgeStream = customerList.stream().map(Customer::getAge).distinct(); List distinctAgeList = distinctAgeStream.collect(Collectors.toList()); assertThat(distinctAgeList, contains(22, 27, 28, 38, 26, 32, 35, 21, 36)); @@ -82,8 +82,8 @@ public void itemsCustomersWantToBuy() { * Create a stream with items' names stored in {@link Customer.wantToBuy} * Use {@link Stream#flatMap} to create a stream from each element of a stream. */ - Function> getItemStream = null; - Stream itemStream = null; + Function> getItemStream = customer -> customer.getWantToBuy().stream(); + Stream itemStream = customerList.stream().flatMap(getItemStream).map(item -> item.getName()); assertTrue(AssertUtil.isLambda(getItemStream)); List itemList = itemStream.collect(Collectors.toList());