From 926beaf0bf2fb126754765dbe3b0431b3b52b50d Mon Sep 17 00:00:00 2001 From: duboisflorian Date: Mon, 27 Mar 2017 16:41:38 +0200 Subject: [PATCH] exercice 1-8a --- .../src/test/java/stream/api/Exercise1Test.java | 8 ++++---- .../src/test/java/stream/api/Exercise2Test.java | 14 +++++++------- .../src/test/java/stream/api/Exercise3Test.java | 11 ++++++----- .../src/test/java/stream/api/Exercise4Test.java | 10 +++++----- .../src/test/java/stream/api/Exercise5Test.java | 16 +++++++--------- .../src/test/java/stream/api/Exercise6Test.java | 4 ++-- .../src/test/java/stream/api/Exercise7Test.java | 9 +++++---- .../src/test/java/stream/api/Exercise8Test.java | 7 +++++-- 8 files changed, 41 insertions(+), 38 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..5268421 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..e4e1b6b 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::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 = (Integer c1, Integer c2) -> (Integer) (c2 - c1); + 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((obj1,obj2) -> obj2.getBudget() - obj1.getBudget()).limit(3).map(Customer::getName); 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::getName); assertTrue(AssertUtil.isLambda(getItemStream)); List itemList = itemStream.collect(Collectors.toList()); diff --git a/stream-api/src/test/java/stream/api/Exercise3Test.java b/stream-api/src/test/java/stream/api/Exercise3Test.java index 04dfdf3..866526c 100644 --- a/stream-api/src/test/java/stream/api/Exercise3Test.java +++ b/stream-api/src/test/java/stream/api/Exercise3Test.java @@ -4,6 +4,7 @@ import common.test.tool.dataset.ClassicOnlineStore; import common.test.tool.entity.Customer; +import common.test.tool.entity.Item; import org.junit.Test; import java.util.Comparator; @@ -23,7 +24,7 @@ public void howManyItemsWanted() { /** * Count how many items there are in {@link Customer.wantToBuy} using {@link Stream#count} */ - long sum = 0L; + long sum = customerList.stream().flatMap(customer -> customer.getWantToBuy().stream()).map(Item::getName).count(); assertThat(sum, is(32L)); } @@ -36,8 +37,8 @@ public void richestCustomer() { * Find the richest customer's budget by using {@link Stream#max} and {@link Comparator#naturalOrder} * Don't use {@link Stream#sorted} */ - Comparator comparator = null; - Optional richestCustomer = null; + Comparator comparator = Comparator.naturalOrder(); + Optional richestCustomer = customerList.stream().map(Customer::getBudget).max(comparator); assertThat(comparator.getClass().getSimpleName(), is("NaturalOrderComparator")); assertThat(richestCustomer.get(), is(12000)); @@ -51,8 +52,8 @@ public void youngestCustomer() { * Find the youngest customer by using {@link Stream#min} * Don't use {@link Stream#sorted} */ - Comparator comparator = null; - Optional youngestCustomer = null; + Comparator comparator = Comparator.comparingInt(Customer::getAge); + Optional youngestCustomer = customerList.stream().min(comparator); assertThat(youngestCustomer.get(), is(customerList.get(8))); } diff --git a/stream-api/src/test/java/stream/api/Exercise4Test.java b/stream-api/src/test/java/stream/api/Exercise4Test.java index 69898e4..82d7443 100644 --- a/stream-api/src/test/java/stream/api/Exercise4Test.java +++ b/stream-api/src/test/java/stream/api/Exercise4Test.java @@ -23,7 +23,7 @@ public void firstRegistrant() { * Find the first customer who registered this online store by using {@link Stream#findFirst} * The customerList are ascending ordered by registered timing. */ - Optional firstCustomer = null; + Optional firstCustomer = customerList.stream().findFirst(); assertThat(firstCustomer.get(), is(customerList.get(0))); } @@ -35,19 +35,19 @@ public void isThereAnyoneOlderThan40() { /** * Check whether any customer older than 40 exists or not, by using {@link Stream#anyMatch} */ - boolean olderThan40Exists = true; + boolean olderThan40Exists = customerList.stream().anyMatch(customer -> customer.getAge()>40); assertThat(olderThan40Exists, is(false)); } @Easy @Test public void isEverybodyOlderThan20() { - List customerList = this.mall.getCustomerList(); + List customerList = this.mall.getCustomerList(); /** * Check whether all customer are older than 20 or not, by using {@link Stream#allMatch} */ - boolean allOlderThan20 = false; + boolean allOlderThan20 = customerList.stream().allMatch(customer -> customer.getAge()>20); assertThat(allOlderThan20, is(true)); } @@ -60,7 +60,7 @@ public void everyoneWantsSomething() { * Confirm that none of the customer has empty list for their {@link Customer.wantToBuy} * by using {@link Stream#noneMatch} */ - boolean everyoneWantsSomething = false; + boolean everyoneWantsSomething = customerList.stream().noneMatch(customer -> customer.getWantToBuy().isEmpty()); assertThat(everyoneWantsSomething, is(true)); } diff --git a/stream-api/src/test/java/stream/api/Exercise5Test.java b/stream-api/src/test/java/stream/api/Exercise5Test.java index dc41f32..c235c9a 100644 --- a/stream-api/src/test/java/stream/api/Exercise5Test.java +++ b/stream-api/src/test/java/stream/api/Exercise5Test.java @@ -6,10 +6,8 @@ import org.junit.Test; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.Set; +import java.util.*; +import java.util.stream.Collector; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -28,7 +26,7 @@ public void nameList() { /** * Create a list of customer names by using {@link Stream#collect} and {@link Collectors#toList} */ - List nameList = null; + List nameList = customerList.stream().map(Customer::getName).collect(Collectors.toList()); assertThat(nameList, contains("Joe", "Steven", "Patrick", "Diana", "Chris", "Kathy", "Alice", "Andrew", "Martin", "Amy")); @@ -41,7 +39,7 @@ public void ageSet() { /** * Create a set of customer age by using {@link Stream#collect} and {@link Collectors#toSet} */ - Set ageSet = null; + Set ageSet = customerList.stream().map(Customer::getAge).collect(Collectors.toSet()); assertThat(ageSet, hasSize(9)); assertThat(ageSet, hasItems(21, 22, 26, 27, 28, 32, 35, 36, 38)); @@ -54,7 +52,7 @@ public void nameInCsv() { /** * Create a csv string of customer names in brackets "[]" by using {@link Collectors#joining} */ - String string = null; + String string = customerList.stream().map(Customer::getName).collect(Collectors.joining(",", "[","]")); assertThat(string, is("[Joe,Steven,Patrick,Diana,Chris,Kathy,Alice,Andrew,Martin,Amy]")); } @@ -67,7 +65,7 @@ public void oldestCustomer() { * Get the oldest customer by using {@link Collectors#maxBy}. * Don't use any intermediate operations. */ - Optional oldestCustomer = null; + Optional oldestCustomer = customerList.stream().collect(Collectors.maxBy( Comparator.comparingInt(Customer::getAge) )); assertThat(oldestCustomer.get(), is(customerList.get(3))); } @@ -80,7 +78,7 @@ public void ageDistribution() { * Create a map of age as key and number of customers as value * using {@link Collectors#groupingBy} and {@link Collectors#counting} */ - Map ageDistribution = null; + Map ageDistribution = customerList.stream().collect(Collectors.groupingBy(Customer::getAge,Collectors.counting())); assertThat(ageDistribution.size(), is(9)); ageDistribution.forEach((k, v) -> { diff --git a/stream-api/src/test/java/stream/api/Exercise6Test.java b/stream-api/src/test/java/stream/api/Exercise6Test.java index 3003eb1..0d7b8db 100644 --- a/stream-api/src/test/java/stream/api/Exercise6Test.java +++ b/stream-api/src/test/java/stream/api/Exercise6Test.java @@ -18,7 +18,7 @@ public void streamFromValues() { /** * Create a stream with string values "a" "b" "c" by using {@link Stream#of} */ - Stream abcStream = null; + Stream abcStream = Stream.of("a", "b", "c"); List abcList = abcStream.collect(Collectors.toList()); assertThat(abcList, contains("a", "b", "c")); @@ -29,7 +29,7 @@ public void numberStream() { /** * Create a stream only with multiples of 3, starting from 0, size of 10, by using {@link Stream#iterate} */ - Stream numbers = null; + Stream numbers = Stream.iterate(0, n -> n + 3 ).limit(10); List numbersList = numbers.collect(Collectors.toList()); assertThat(numbersList, contains(0, 3, 6, 9, 12, 15, 18, 21, 24, 27)); diff --git a/stream-api/src/test/java/stream/api/Exercise7Test.java b/stream-api/src/test/java/stream/api/Exercise7Test.java index 6eec18a..bf02976 100644 --- a/stream-api/src/test/java/stream/api/Exercise7Test.java +++ b/stream-api/src/test/java/stream/api/Exercise7Test.java @@ -3,6 +3,7 @@ import common.test.tool.annotation.Easy; import common.test.tool.dataset.ClassicOnlineStore; import common.test.tool.entity.Customer; +import common.test.tool.entity.Item; import common.test.tool.entity.Shop; import org.junit.Test; @@ -26,8 +27,8 @@ public void averageAge() { * Create {@link IntStream} with customer ages by using {@link Stream#mapToInt} * Then calculate the average of ages by using {@link IntStream#average} */ - IntStream ageStream = null; - OptionalDouble average = null; + IntStream ageStream = customerList.stream().mapToInt(Customer::getAge); + OptionalDouble average = ageStream.average(); assertThat(average.getAsDouble(), is(28.7)); } @@ -40,8 +41,8 @@ public void howMuchToBuyAllItems() { * Create {@link LongStream} with all items' prices using {@link Stream#mapToLong} * Then calculate the sum of prices using {@link LongStream#sum} */ - LongStream priceStream = null; - long priceSum = 0; + LongStream priceStream = shopList.stream().flatMap(shop -> shop.getItemList().stream()).mapToLong(Item::getPrice); + long priceSum = priceStream.sum(); assertThat(priceSum, is(60930L)); } diff --git a/stream-api/src/test/java/stream/api/Exercise8Test.java b/stream-api/src/test/java/stream/api/Exercise8Test.java index fb2c91a..90ba8da 100644 --- a/stream-api/src/test/java/stream/api/Exercise8Test.java +++ b/stream-api/src/test/java/stream/api/Exercise8Test.java @@ -8,9 +8,12 @@ import org.junit.Test; +import java.util.Comparator; import java.util.List; import java.util.Set; import java.util.function.Predicate; +import java.util.stream.Collector; +import java.util.stream.Collectors; import java.util.stream.Stream; import static org.hamcrest.Matchers.hasItems; @@ -27,8 +30,8 @@ public void itemsNotOnSale() { /** * Create a set of item names that are in {@link Customer.wantToBuy} but not on sale in any shop. */ - List itemListOnSale = null; - Set itemSetNotOnSale = null; + List itemListOnSale = shopStream.map(Shop::getItemList).flatMap(List::stream).map(Item::getName).collect(Collectors.toList()); + Set itemSetNotOnSale = customerStream.map(Customer::getWantToBuy).flatMap(List::stream).map(Item::getName).filter(item -> !itemListOnSale.contains(item)).collect(Collectors.toSet()); assertThat(itemSetNotOnSale, hasSize(3)); assertThat(itemSetNotOnSale, hasItems("bag", "pants", "coat"));