Skip to content

commit de l'exercice 1 et 2 non terminé #15

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 1 commit 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
8 changes: 4 additions & 4 deletions stream-api/src/test/java/stream/api/Exercise1Test.java
100644 → 100755
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,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<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());
Expand Down
14 changes: 7 additions & 7 deletions stream-api/src/test/java/stream/api/Exercise2Test.java
100644 → 100755
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 = ((o1, o2) -> o1 - o2);
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,7 @@ 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().sorted((o1, o2) -> o1.getBudget()).map(Customer::getName).limit(3);

List<String> top3RichCustomerList = top3RichCustomerStream.collect(Collectors.toList());
assertThat(top3RichCustomerList, contains("Diana", "Andrew", "Chris"));
Expand All @@ -68,7 +68,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 +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<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 -> item.getName());

assertTrue(AssertUtil.isLambda(getItemStream));
List<String> itemList = itemStream.collect(Collectors.toList());
Expand Down