Skip to content

Commit 34cc0db

Browse files
author
Daniel Reuter
committed
Solutions
1 parent 6b2922d commit 34cc0db

1 file changed

Lines changed: 32 additions & 15 deletions

File tree

src/main/java/de/codecentric/java8examples/streaming/FilteringAndMapping.java

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package de.codecentric.java8examples.streaming;
22

3+
import java.time.LocalDate;
4+
import java.time.Period;
5+
import java.util.List;
6+
import java.util.Set;
7+
import java.util.stream.Collectors;
8+
import java.util.stream.Stream;
9+
310
import de.codecentric.java8examples.Invoice;
411
import de.codecentric.java8examples.InvoiceItem;
512
import de.codecentric.java8examples.Person;
613

7-
import java.util.Collections;
8-
import java.util.List;
9-
import java.util.Set;
10-
1114
/**
1215
* Your task: Implement the following methods and make the tests passs.
1316
*/
@@ -17,44 +20,58 @@ public class FilteringAndMapping {
1720
* Extract a list of names (firstname and lastname separated by space) from a given list of Person objects.
1821
*/
1922
public static List<String> extractNames(List<Person> persons) {
20-
return Collections.emptyList();
23+
return persons.stream()//
24+
.<String> map(p -> p.getFirstName() + " " + p.getLastName())//
25+
.collect(Collectors.<String> toList());
2126
}
2227

2328
/**
24-
* Extract a sorted (ascending by lastname) list of names (firstname and lastname separated by space) from a
25-
* given list of Person objects.
29+
* Extract a sorted (ascending by lastname) list of names (firstname and lastname separated by space) from a given list of Person objects.
2630
*/
2731
public static List<String> extractNamesSortedByLastname(List<Person> persons) {
28-
return Collections.emptyList();
32+
return persons.stream()//
33+
.sorted((Person p1, Person p2) -> (p1.getLastName().compareTo(p2.getLastName())))//
34+
.<String> map(p -> p.getFirstName() + " " + p.getLastName())//
35+
.collect(Collectors.<String> toList());
2936
}
3037

3138
/**
3239
* From a given list of Person objects, extract a list of female firstnames
3340
*/
3441
public static List<String> extractFemaleFirstnames(List<Person> persons) {
35-
return Collections.emptyList();
42+
return persons.stream()//
43+
.filter(p -> p.getGender().equals(Person.Gender.FEMALE))//
44+
.<String> map(p -> p.getFirstName())//
45+
.collect(Collectors.<String> toList());
3646
}
3747

3848
/**
3949
* Extract all females older than 18 years from a given list of Person objects.
4050
*/
4151
public static List<Person> extractAdultWomen(List<Person> persons) {
42-
return Collections.emptyList();
52+
return persons.stream()//
53+
.filter(p -> p.getGender().equals(Person.Gender.FEMALE))//
54+
.filter((Person p) -> Period.between(p.getBirthDay(), LocalDate.now()).getYears() >= 18)//
55+
.collect(Collectors.<Person> toList());
4356
}
4457

4558
/**
46-
* From a given list of Person objects, extract a set of firstnames of the people whose lastname starts
47-
* with the given string.
59+
* From a given list of Person objects, extract a set of firstnames of the people whose lastname starts with the given string.
4860
*/
4961
public static Set<String> extractFirstnamesWhereLastnameStartsWith(List<Person> persons, String startsWith) {
50-
return Collections.emptySet();
62+
return persons.stream()//
63+
.filter((Person p) -> p.getLastName().startsWith(startsWith))//
64+
.<String> map(p -> p.getFirstName())//
65+
.collect(Collectors.<String> toSet());
5166
}
5267

5368
/**
5469
* From a given list of invoices, extract a set of all product names.
5570
*/
5671
public static Set<String> extractAllProducts(List<Invoice> invoices) {
57-
return Collections.emptySet();
72+
return invoices.stream()//
73+
.<InvoiceItem> flatMap((Invoice i) -> i.getItems().stream())//
74+
.<String> map((InvoiceItem i) -> i.getProduct())//
75+
.collect(Collectors.<String> toSet());
5876
}
5977
}
60-

0 commit comments

Comments
 (0)