Back
Close

Java Guild Meeting 5/2018

Gi11i4m
4,840 views

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
// { autofold
package streams;
import java.util.*;
import static java.util.stream.Collectors.*;
public class FlatMap {
// }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Get the oldest person from the collection
// { autofold
package streams;
import java.util.*;
public class MaxAndComparator {
// }
public static Person getOldestPerson(List<Person> people) {
Person oldestPerson = new Person("", 0);
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Sum all elements of a collection, try to use the reduce operator with identity parameter instead of an IntStream
// { autofold
package streams;
import java.util.*;
public class SumAndReduce {
// }
public static int calculate(List<Integer> numbers) {
int total = 0;
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Get the names of all kids under the age of 18
// { autofold
package streams;
import java.util.*;
public class UnderAge {
// }
public static Set<String> getKidNames(List<Person> people) {
Set<String> kids = new HashSet<>();
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
// { autofold
package streams;
import java.util.*;
public class PartitionBy {
// }
public static Map<Boolean, List<Person>> partitionAdults(List<Person> people) {
Map<Boolean, List<Person>> map = new HashMap<>();
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Group these people by nationality, same kind as the previous exercise
// { autofold
package streams;
import java.util.*;
public class GroupBy {
// }
public static Map<String, List<Person>> groupByNationality(List<Person> people) {
Map<String, List<Person>> map = new HashMap<>();
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Return a comma-separated string of all these people's names
// { autofold
package streams;
import java.util.*;
public class Joining {
// }
public static String namesToString(List<Person> people) {
String label = "Names: ";
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
// { autofold
package streams;
import java.util.*;
import static java.util.stream.Collectors.*;
public class Challenge {
// }
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