Back
Close

Java Guild Meeting 5/2018

Gi11i4m
3,169 views
Previous: Streams - Practice Next: Streams - Summary

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
// {...}
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
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Get the oldest person from the collection
// {...}
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
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Sum all elements of a collection, try to use the reduce operator with identity parameter instead of an IntStream
// {...}
public static int calculate(List<Integer> numbers) {
int total = 0;
for (int number : numbers) {
total += number;
}
return total;
}
// {...}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Get the names of all kids under the age of 18
// {...}
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
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
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
// {...}
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
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Group these people by nationality, same kind as the previous exercise
// {...}
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
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Return a comma-separated string of all these people's names
// {...}
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
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
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
// {...}
public static String getString(List<Integer> list) {
return null;
}
// {...}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants