Java 8 Streams: Micro Katas

A programming kata is an exercise which helps a programmer hone his skills through practice and repetition.

This article is part of the series Java Tutorial Through Katas.

The article assumes that the reader already has experience with Java, that he is familiar with the basics of unit tests and that he knows how to run them from his favorite IDE (mine is IntelliJ IDEA).

The idea behind exercises displayed below is to learn Java 8 Streaming using test-driven development approach (write the implementation for the first test, confirm that it passes and move to the next).

Each section will start with an objective in form of tests that prove that the implementation will be correct once it’s written. Each of those tests are followed with one possible implementation in Java 7 (or earlier) and Java 8 using Streams. That way reader can compare some of the new features of Java 8 with their equivalents in earlier JDKs. Please try to solve tests without looking at provided solutions.

For more information about TDD best practices, please read the Test Driven Development (TDD): Best Practices Using Java Examples.

Java 8 map

Convert elements of a collection to upper case.

Tests

Java 7 (transform7) and Java8 (transform) Implementations

Java 8 filter

Filter collection so that only elements with less than 4 characters are returned.

Tests

Java 7 (transform7) and Java8 (transform) Implementations

Java 8 flatMap

Flatten multidimensional collection

Tests

Java 7 (transform7) and Java8 (transform) Implementations

Java 8 max and comparator

Get oldest person from the collection

Tests

Java 7 (getOldestPerson7) and Java8 (getOldestPerson) Implementations

Java 8 sum and reduce

Sum all elements of a collection

Tests

Java 7 (calculate7) and Java8 (calculate) Implementations

Java 8 filter and map

Get names of all kids (under age of 18)

Tests

Java 7 (getKidNames7) and Java8 (getKidNames) Implementations

Java 8 summaryStatistics

Get people statistics: average age, count, maximum age, minimum age and sum og all ages.

Tests

Java 7 (getStats7) and Java8 (getStats) Implementations

Java 8 partitioningBy

Partition adults and kids

Tests

Java 7 (partitionAdults7) and Java8 (partitionAdults) Implementations

Java 8 groupingBy

Group people by nationality

Tests

Java 7 (groupByNationality7) and Java8 (groupByNationality) Implementations

Java 8 joining

Return people names separated by comma

Tests

Java 7 (namesToString7) and Java8 (namesToString) Implementations

Source

Full source is located in the GitHub repo vfarcic/java-8-exercises. Besides tests and implementation, repository includes build.gradle that can be used, among other things, to download AssertJ dependencies and run tests.

Test-Driven Java Development

7429OS_Test-Driven Java DevelopmentTest-Driven Java Development book wrote by Alex Garcia and me has been published by Packt Publishing. It was a long, demanding, but very rewarding journey that resulted in a very comprehensive hands-on material for all Java developers interested in learning or improving their TDD skills.

If you liked this article I am sure that you’ll find this book very useful. It contains extensive tutorials, guidelines and exercises for all Java developers eager to learn how to successfully apply TDD practices.

You can download a sample or purchase your own copy directly from Packt or Amazon.

Advertisements

6 thoughts on “Java 8 Streams: Micro Katas

  1. Pingback: Java 8 Streams: Micro Katas | Java 8

  2. manuel

    Great post, thanks.
    The test name for “Java 8 sum and reduce” transformShouldConvertCollectionElementsToUpperCase()
    Does not seem the best choice

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s