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 invokedreverseEachStringon 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
StringBuilderover the stream, to get a stream ofStringBuilders, then map thereversemethod ofStringBuilderover this stream, then map thetoStringmethod ofStringBuilderover the resulting stream to get a stream ofStrings 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
inputinto 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.sqrtfunction, 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 tomapand one call tofilter(as well as the usual calls tostreamandcollect). -
Write a
mainmethod to demonstrate that your static methods work on some example inputs.