Skip to content

dominique dore exo 1 et 2 #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions stream-api/src/test/java/stream/api/Exercise1Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Customer> richCustomerCondition = null;
Stream<Customer> richCustomerStream = null;
Predicate<Customer> richCustomerCondition = customer -> customer.getBudget() > 10000;
Stream<Customer> richCustomerStream = customerList.stream().filter(richCustomerCondition);

assertTrue("Solution for Predicate should be lambda expression", AssertUtil.isLambda(richCustomerCondition));
List<Customer> richCustomer = richCustomerStream.collect(Collectors.toList());
Expand All @@ -45,12 +45,12 @@ 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<Customer, Integer> getAgeFunction = null;
Stream<Integer> ageStream = null;
Function<Customer, Integer> getAgeFunction = Customer::getAge;
Stream<Integer> ageStream = customerList.stream().map(getAgeFunction);

assertTrue(AssertUtil.isLambda(getAgeFunction));
List<Integer> richCustomer = ageStream.collect(Collectors.toList());
assertThat(richCustomer, hasSize(10));
assertThat(richCustomer, contains(22, 27, 28, 38, 26, 22, 32, 35, 21, 36));
}
}
}
18 changes: 10 additions & 8 deletions stream-api/src/test/java/stream/api/Exercise2Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void sortByAge() {
* Create a stream with ascending ordered age values.
* Use {@link Stream#sorted} to sort them.
*/
Stream<Integer> sortedAgeStream = null;
Stream<Integer> sortedAgeStream = customerList.stream().map(Customer::getAge).sorted();

List<Integer> sortedAgeList = sortedAgeStream.collect(Collectors.toList());
assertThat(sortedAgeList, contains(21, 22, 22, 26, 27, 28, 32, 35, 36, 38));
Expand All @@ -40,8 +40,8 @@ public void descSortByAge() {
/**
* Create a stream with descending ordered age values.
*/
Comparator<Integer> descOrder = null;
Stream<Integer> sortedAgeStream = null;
Comparator<Integer> descOrder = (obj1, obj2) -> obj2-obj1;
Stream<Integer> sortedAgeStream = customerList.stream().map(Customer::getAge).sorted(descOrder);

assertTrue(AssertUtil.isLambda(descOrder));
List<Integer> sortedAgeList = sortedAgeStream.collect(Collectors.toList());
Expand All @@ -55,7 +55,9 @@ public void top3RichCustomer() {
/**
* Create a stream with top 3 rich customers using {@link Stream#limit} to limit the size of the stream
*/
Stream<String> top3RichCustomerStream = null;
Stream<String> top3RichCustomerStream = customerList.stream().filter(
customer -> customer.getBudget() > 10000
).map(Customer::getName).limit(3);

List<String> top3RichCustomerList = top3RichCustomerStream.collect(Collectors.toList());
assertThat(top3RichCustomerList, contains("Diana", "Andrew", "Chris"));
Expand All @@ -68,7 +70,7 @@ public void distinctAge() {
/**
* Create a stream with distinct age values using {@link Stream#distinct}
*/
Stream<Integer> distinctAgeStream = null;
Stream<Integer> distinctAgeStream = customerList.stream().map(Customer::getAge).distinct();

List<Integer> distinctAgeList = distinctAgeStream.collect(Collectors.toList());
assertThat(distinctAgeList, contains(22, 27, 28, 38, 26, 32, 35, 21, 36));
Expand All @@ -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<Customer, Stream<Item>> getItemStream = null;
Stream<String> itemStream = null;
Function<Customer, Stream<Item>> getItemStream = customer -> customer.getWantToBuy().stream();
Stream<String> itemStream = customerList.stream().flatMap(getItemStream).map(Item::getName);

assertTrue(AssertUtil.isLambda(getItemStream));
List<String> itemList = itemStream.collect(Collectors.toList());
Expand All @@ -93,4 +95,4 @@ public void itemsCustomersWantToBuy() {
"plane", "bag", "cold medicine", "chair", "desk", "pants", "coat", "cup", "plate", "fork",
"spoon", "ointment", "poultice", "spinach", "ginseng", "onion"));
}
}
}
13 changes: 7 additions & 6 deletions stream-api/src/test/java/stream/api/Exercise3Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()).count();

assertThat(sum, is(32L));
}
Expand All @@ -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<Integer> comparator = null;
Optional<Integer> richestCustomer = null;
Comparator<Integer> comparator = Comparator.naturalOrder();
Optional<Integer> richestCustomer = customerList.stream().map(Customer::getBudget).max(comparator);

assertThat(comparator.getClass().getSimpleName(), is("NaturalOrderComparator"));
assertThat(richestCustomer.get(), is(12000));
Expand All @@ -51,9 +52,9 @@ public void youngestCustomer() {
* Find the youngest customer by using {@link Stream#min}
* Don't use {@link Stream#sorted}
*/
Comparator<Customer> comparator = null;
Optional<Customer> youngestCustomer = null;
Comparator<Customer> comparator = Comparator.comparing(Customer::getAge);
Optional<Customer> youngestCustomer = customerList.stream().min(comparator);

assertThat(youngestCustomer.get(), is(customerList.get(8)));
}
}
}
10 changes: 5 additions & 5 deletions stream-api/src/test/java/stream/api/Exercise4Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Customer> firstCustomer = null;
Optional<Customer> firstCustomer = customerList.stream().findFirst();

assertThat(firstCustomer.get(), is(customerList.get(0)));
}
Expand All @@ -35,7 +35,7 @@ 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));
}
Expand All @@ -47,7 +47,7 @@ public void isEverybodyOlderThan20() {
/**
* Check whether all customer are older than 20 or not, by using {@link Stream#allMatch}
*/
boolean allOlderThan20 = false;
boolean allOlderThan20 = customerList.stream().anyMatch(customer -> customer.getAge() > 20);

assertThat(allOlderThan20, is(true));
}
Expand All @@ -60,8 +60,8 @@ 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));
}
}
}
17 changes: 7 additions & 10 deletions stream-api/src/test/java/stream/api/Exercise5Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@

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.Collectors;
import java.util.stream.Stream;

Expand All @@ -28,7 +25,7 @@ public void nameList() {
/**
* Create a list of customer names by using {@link Stream#collect} and {@link Collectors#toList}
*/
List<String> nameList = null;
List<String> nameList = customerList.stream().map(Customer::getName).collect(Collectors.toList());

assertThat(nameList, contains("Joe", "Steven", "Patrick", "Diana", "Chris", "Kathy", "Alice", "Andrew",
"Martin", "Amy"));
Expand All @@ -41,7 +38,7 @@ public void ageSet() {
/**
* Create a set of customer age by using {@link Stream#collect} and {@link Collectors#toSet}
*/
Set<Integer> ageSet = null;
Set<Integer> 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));
Expand All @@ -54,7 +51,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]"));
}
Expand All @@ -67,7 +64,7 @@ public void oldestCustomer() {
* Get the oldest customer by using {@link Collectors#maxBy}.
* Don't use any intermediate operations.
*/
Optional<Customer> oldestCustomer = null;
Optional<Customer> oldestCustomer = customerList.stream().collect(Collectors.maxBy(Comparator.comparing(Customer::getAge)));

assertThat(oldestCustomer.get(), is(customerList.get(3)));
}
Expand All @@ -80,7 +77,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<Integer, Long> ageDistribution = null;
Map<Integer, Long> ageDistribution = customerList.stream().collect(Collectors.groupingBy(Customer::getAge, Collectors.counting()));

assertThat(ageDistribution.size(), is(9));
ageDistribution.forEach((k, v) -> {
Expand All @@ -91,4 +88,4 @@ public void ageDistribution() {
}
});
}
}
}
6 changes: 3 additions & 3 deletions stream-api/src/test/java/stream/api/Exercise6Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void streamFromValues() {
/**
* Create a stream with string values "a" "b" "c" by using {@link Stream#of}
*/
Stream<String> abcStream = null;
Stream<String> abcStream = Stream.of("a", "b", "c");

List<String> abcList = abcStream.collect(Collectors.toList());
assertThat(abcList, contains("a", "b", "c"));
Expand All @@ -29,9 +29,9 @@ public void numberStream() {
/**
* Create a stream only with multiples of 3, starting from 0, size of 10, by using {@link Stream#iterate}
*/
Stream<Integer> numbers = null;
Stream<Integer> numbers = Stream.iterate(0, n -> n+3).limit(10);

List<Integer> numbersList = numbers.collect(Collectors.toList());
assertThat(numbersList, contains(0, 3, 6, 9, 12, 15, 18, 21, 24, 27));
}
}
}