Stream Processing in Java 8
Introduction
For each of the following exercises provide two solutions. The first solution must use the standard iteration constructs of Java; the second solution must use methods of the Stream interface. Futhermore, use Java generics whenever possible.
Exercise 1
Write a static method sumOdd
that given a List
of integers computes
the sum of the values that are odd.
Goal: Warming up!
Expected output: A properly commented Java source file containing the two requested methods.
Exercise 2
Write a static method repl
that given an array xs
of Object
and a integer
n
returns an array containing the elements of xs
replicated n
times in any order.
Hint: For the stream-based version, consider the flatMap
method of Stream
.
Goal: Exploring the Stream API further and its relation with types in Java.
Expected output: A properly commented Java source file implementing the requested method.
Exercise 3
Write a static
method titlecase
that given a string s
converts it
to titlecase by upper-casing the first letter of every word.
Goal: Re-implementing an old Haskell exercise in Java, comparing the solutions.
Expected output: A properly commented Java source file implementing the requested method.
Exercise 4
Implement a generic class ImmutablePair<T1,T2>
.
Besides a suitable constructor your class should provide also methods to get each element individually.
Write a static method zipWithIndex
that given an array of type T
returns a Stream<ImmutablePair<T, Integer>>
containing the elements of
the array with its indexes. Use the method just defined to implement
another static method filterOdd
that given an array xs
returns a new
array obtained from xs
by removing the elements at odd positions.
Hint: Here "odd positions" means the first, third, fifth, etc position.
Goal: Re-implementing an old Haskell exercise in Java, comparing the solutions (pt. II).
Expected output: A properly commented Java source file implementing the requested method.
Exercise 5
Write a static method replaceWord
that given the name of a text file
(String fileName
), a word (String word
) and a replacement
(String repl
), prints the content of fileName
where each occurrence
of word
is replaced with repl
. Note that the structure, i.e. lines,
of fileName
must be preserved. Test your method with the file exercises_5.html
,
replacing for example Exercise
with Problem
.
Hint: Consider the method Files.lines of the Java API.
Goal: Working with more advanced aspects of Java's Stream API.
Expected output: A properly commented Java source file implementing the requested method.
Exercise 6
Implement a static method serialEvenSum
that given a
long threshold
as parameter computes the sum of even numbers up to
the threshold using the Stream API.
Futhermore, write a static method parallelEvenSum
that does the same but using a parallel stream.
Finally, write another static method testSum
that takes a long threshold
as argument, runs the two methods just defined, measures and compares their running time. Invoke testSum
for increasing values of the threshold
and determine for which values of threshold
using serial streams is better than using the parallel ones and vice versa.
Hint: Consider the method System.nanoTime to measure time.
Goal: Working with more advanced aspects (parallelism) of Java's Stream API.
Expected output: A properly commented Java source file implementing the requested method.
Exercise 7
Write a method getElement
implementing a partial function that given
an array arr
and an int index
returns arr[index]
if defined.
Use Optional as the result type.
Next write methods implementing the following partial functions:
sqrt
, that applied to an integer returns its square root as a double, if the argument is not negative;half
, that applied to an integer returns its half as an integer, if the argument is even.
Finally write a method that composing getElement
, sqrt
and half
,
when applied to an array of integers arr
and to an int index
,
returns the square root of the half of arr[index]
, if defined.
Goal: Working with partial functions in Java, exploiting the Optional
class.
Expected output: A properly commented Java source file implementing the requested methods.
Exercise 8
Write a static method listDir
that given the path of a directory as
argument returns a List<String>
containing the names of all the
subdirectories recursively contained in it.
Hint: Consider to use the methods of the
Files
class, e.g. list
or walk
.
Goal: Accessing system's informations though Java.
Expected output: A properly commented Java source file implementing the requested method.
Goal: Working with more advanced aspects of Java's Stream API.
Expected output: A properly commented Java source file implementing the requested method.