Java Guild Meeting 5/2018

Gi11i4m
2,084 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Micro kata's

Here are a few micro kata's for you to solve, let's get to know some more possibilities of Streams!

If you want to know if your solution is optimal you can take look at the source & solutions.

The kata's are written in old-style Java code. Re-write them into beautiful one-liners using Streams.

alt text

Remember, you can find the documentation on Streams right here.

Flatten this multidimensional collection
1
10
11
12
13
14
15
16
17
18
19
20
21
// {...}
public static List<String> transform(List<List<String>> collection) {
List<String> newCollection = new ArrayList<>();
for (List<String> subCollection : collection) {
for (String value : subCollection) {
newCollection.add(value);
}
}
return newCollection;
}
// {...}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Get the oldest person from the collection
1
8
9
10
11
12
13
14
15
16
17
18
19
// {...}
public static Person getOldestPerson(List<Person> people) {
Person oldestPerson = new Person("", 0);
for (Person person : people) {
if (person.getAge() > oldestPerson.getAge()) {
oldestPerson = person;
}
}
return oldestPerson;
}
// {...}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Sum all elements of a collection, try to use the reduce operator with identity parameter instead of an IntStream
1
8
9
10
11
12
13
14
15
16
17
// {...}
public static int calculate(List<Integer> numbers) {
int total = 0;
for (int number : numbers) {
total += number;
}
return total;
}
// {...}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Get the names of all kids under the age of 18
1
8
9
10
11
12
13
14
15
16
17
18
19
// {...}
public static Set<String> getKidNames(List<Person> people) {
Set<String> kids = new HashSet<>();
for (Person person : people) {
if (person.getAge() < 18) {
kids.add(person.getName());
}
}
return kids;
}
// {...}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Not very interesting as an exercise, but check out this example of how to easily generate numerical statistics from an IntStream.

Partition these people into adults and kids, you'll need a special collector for this one
1
8
9
10
11
12
13
14
15
16
17
18
19
// {...}
public static Map<Boolean, List<Person>> partitionAdults(List<Person> people) {
Map<Boolean, List<Person>> map = new HashMap<>();
map.put(true, new ArrayList<>());
map.put(false, new ArrayList<>());
for (Person person : people) {
map.get(person.getAge() >= 18).add(person);
}
return map;
}
// {...}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Group these people by nationality, same kind as the previous exercise
1
8
9
10
11
12
13
14
15
16
17
18
19
20
// {...}
public static Map<String, List<Person>> groupByNationality(List<Person> people) {
Map<String, List<Person>> map = new HashMap<>();
for (Person person : people) {
if (!map.containsKey(person.getNationality())) {
map.put(person.getNationality(), new ArrayList<>());
}
map.get(person.getNationality()).add(person);
}
return map;
}
// {...}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Return a comma-separated string of all these people's names
1
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// {...}
public static String namesToString(List<Person> people) {
String label = "Names: ";
StringBuilder sb = new StringBuilder(label);
for (Person person : people) {
if (sb.length() > label.length()) {
sb.append(", ");
}
sb.append(person.getName());
}
sb.append(".");
return sb.toString();
}
// {...}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Challenge

Write a method that returns a comma separated string based on a given list of integers. Each element should be preceded by the letter 'e' if the number is even, and preceded by the letter 'o' if the number is odd. For example, if the input list is (3,44), the output should be 'o3,e44'.

You can write this using only one operator and one collect statement. Go here and click 'Solution' if you want to see the optimal solution.

Return a comma-separated string of all these people's names
1
10
11
12
13
14
15
// {...}
public static String getString(List<Integer> list) {
return null;
}
// {...}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content