Reminder to functional programming elements in Java.

exercise No. 16

Create comment in forum


As a courtesy we remind all participants to functional programming elements being dealt with in Softwareentwicklung 2.

This exercise has been derived from Lambda Expressions - The Java™ Tutorials. Depending on your own personal knowledge you may want to start that trail beforehand.

Import the following project template into Intellij IDEA:

This template contains:

  1. Some boilerplate code in de.hdm_stuttgart.mi.javastreams.RosterTest modified from Lambda Expressions - The Java™ Tutorials. Execution does make sense when reading Oracle's trail in parallel.

  2. A unit test class de.hdm_stuttgart.mi.javastreams.Java8FunctionalTest currently being masked by an @Ignore annotation to be removed for enabling tests. This is your place to actually start working.

The project template already contains unit tests. Your task is providing the implementation generating the expected results.

The first test of de.hdm_stuttgart.mi.javastreams.Java8FunctionalTest already being completed is intended to serve as a blueprint:

 * Order all male students by email and create a List<String> of their respective
 * names in that order eliminating possible duplicates: 
 *  "Bob", 2, Student.Sex.MALE, ""  
 *  "Fred", 2, Student.Sex.MALE, ""
 *  "George", 4, Student.Sex.MALE, ""
 *  "Jane", 1, Student.Sex.FEMALE, ""
 *  "Kim", 2, Student.Sex.FEMALE, ""
 *  ==> {"Bob", "Fred", "George"} 
public void allMaleDistinctNameOrderedByEmail() {

  final List<String> emails = 
            filter(s -> s.gender == Sex.MALE).
            sorted((s1, s2) -> s1.getEmailAddress().compareTo(s2.getEmailAddress())).

    Matchers.<List<String>> equalTo(
                  ImmutableList.of("Bob", "Fred", "George")

An informal description of the desired outcome.

A list of records illustrating a processing step.

An informal description of the desired output.

A stream pipeline yielding the result.

A unit test checking for correctness.

A variable like email in the given example will be set to null in all remaining tests. It is your task assigning these variables accordingly so that all tests pass as in:

public void markSumAllStudentsMarksHavingEmail_Dot_de() {

  final int sumMarksOfStudentsHavingGermanEmail = 123; /* TODO: Correct me!*/

  assertThat(sumMarksOfStudentsHavingGermanEmail, Matchers.equalTo(3));

Thus the variable sumMarksOfStudentsHavingGermanEmail requires an assignment correction based on students sample data.

The class de.hdm_stuttgart.mi.javastreams.RosterTest contains a main() method also shows related sample code.


Unit tests for this type of work frequently require comparing List and Map objects. The above code uses Guava for creating immutable collections representing expected outcomes.

On the other end Hamcrest allows for using these collections to be compared with actual test outcomes. The test public void studentNamesBySex() contains a more complex example constructing a Map.

Larger numbers of immutable collection items may require using the put() method being described in the topmost example of ImmutableMap.Builder rather than merely a constructor.