From 4fffcb62c64d6b2e7e06d73456b75b14226c0882 Mon Sep 17 00:00:00 2001 From: Houssem Date: Mon, 27 Mar 2017 16:27:27 +0200 Subject: [PATCH] Houssem Eddine BOUSSORA --- .../src/test/java/stream/api/Exercise1Test.java | 8 ++++---- .../src/test/java/stream/api/Exercise2Test.java | 16 +++++++++------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/stream-api/src/test/java/stream/api/Exercise1Test.java b/stream-api/src/test/java/stream/api/Exercise1Test.java index 368443e..55a7987 100644 --- 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 index a7f8956..fb84422 100644 --- 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 -> 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,9 @@ public void descSortByAge() { /** * Create a stream with descending ordered age values. */ - Comparator descOrder = null; - Stream sortedAgeStream = null; + + Comparator descOrder = (o1, o2) -> o2 - o1; + Stream sortedAgeStream = customerList.stream().map(customer -> customer.getAge()).sorted(descOrder); assertTrue(AssertUtil.isLambda(descOrder)); List sortedAgeList = sortedAgeStream.collect(Collectors.toList()); @@ -55,7 +56,8 @@ 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; + Comparator descOrder = (o1, o2) -> o2.getBudget() - o1.getBudget(); + Stream top3RichCustomerStream = customerList.stream().sorted(descOrder).map(customer -> customer.getName()).limit(3); List top3RichCustomerList = top3RichCustomerStream.collect(Collectors.toList()); assertThat(top3RichCustomerList, contains("Diana", "Andrew", "Chris")); @@ -68,7 +70,7 @@ public void distinctAge() { /** * Create a stream with distinct age values using {@link Stream#distinct} */ - Stream distinctAgeStream = null; + Stream distinctAgeStream = customerList.stream().map(customer -> customer.getAge()).distinct(); List distinctAgeList = distinctAgeStream.collect(Collectors.toList()); assertThat(distinctAgeList, contains(22, 27, 28, 38, 26, 32, 35, 21, 36)); @@ -82,8 +84,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::getName); assertTrue(AssertUtil.isLambda(getItemStream)); List itemList = itemStream.collect(Collectors.toList());