This question is about writing static methods that use the map
and filter
operations on streams.
-
Create a class,
Example
, that we will use as a placeholder for the static methods that you will write. -
Write a static method with the following signature:
static List reverseEachString(List input);
This method should return a list whose contents are identical to the strings in
input
, except that each string should be reversed. For example, if you invokedreverseEachString
on the list[ "hello", "world" ]
, you should get back the list[ "olleh", "dlrow" ]
.Your implementation should comprise a single statement that should get a stream from the list, apply multiple map operations to the stream, and collect the result back into a list.
In particular, you should map the constructor of
StringBuilder
over the stream, to get a stream ofStringBuilder
s, then map thereverse
method ofStringBuilder
over this stream, then map thetoString
method ofStringBuilder
over the resulting stream to get a stream ofString
s again. -
Now write an alternative version of
reverseEachString
, calledreverseEachStringMonolithic
. This should behave in a functionally identical manner toreverseEachString
, but instead of applying multiple map operations, a single map operation should be used that combines the effects of all of the map operations you used inreverseEachString
. -
Write a static method with the following signature:
static List sqrtsOfFirstDigits(List input);
This method should turns
input
into a stream, filter to just those strings that start with a digit, map each remaining string to the integer corresponding to its first digit, and then map each such integer to its square root via theMath.sqrt
function, returning the resulting stream collected as a list. -
Now write an alternative version of
sqrtsOfFirstDigits
, calledsqrtsOfFirstDigitsMonolithic
. This should have the same behaviour assqrtsOfFirstDigits
, but should use just one call tomap
and one call tofilter
(as well as the usual calls tostream
andcollect
). -
Write a
main
method to demonstrate that your static methods work on some example inputs.