The purpose of this question is to give you practice writing reductions over streams, both with and without identities.
-
Create a class,
Example
, that we will use as a placholder for the static methods that you will write. -
List concatenation: Write a static method with the following signature:
static List<Integer> concatenate(List<List<Integer>> lists);
The method should stream
lists
, to yield a stream of lists of integers, and then should reduce this to a single list, which should be returned. The single list should be the concatenation of all the list. You should use a reduction with an identity here, and iflists
is empty, the result ofconcatenate
should be an empty list. -
Find min with identity: Write a static method with the following signature:
static int findMin(List<Integer> numbers);
The method should stream
numbers
and perform a reduction with identity to yield the smallest integer in the list, or the largest possible integer if the list is empty. Your reduction should use a method reference as its accumulator. -
Find min without identity: Write a static method with the following signature:
static int findMinOrZero(List<Integer> numbers);
The method should stream
numbers
and perform a reduction without an identity to yield the smallest integer in the list, or zero if the list is empty. Your reduction should use a method reference as its accumulator. -
Find max with identity: Write a static method with the following signature:
static int findMax(List<Integer> numbers);
The method should stream
numbers
and perform a reduction with identity to yield the largest integer in the list, or the smallest possible integer if the list is empty. For the sake of practice, your reduction should use a lambda as its accumulator (even though a method reference would be more natural). -
Find max without identity: Write a static method with the following signature:
static int findMaxOrZero(List<Integer> numbers);
The method should stream
numbers
and perform a reduction without an identity to yield the largest integer in the list, or zero if the list is empty. For the sake of practice, your reduction should use a lambda as its accumulator (even though a method reference would be more natural). -
Find min of maxes: Write a static method with the following signature:
static int findMinOfMaxes(List<List<Integer>> listOfLists);
This method should re-use
findMin
andfindMax
to return the smallest maximum among each list inlistOfLists
. -
Demo code: Write a
main
method to demonstrate that your implementations of these methods works on some example inputs. For example, you could use these list declarations and invocations of the above methods, work out what the expected results should be and check whether your solution does give these results:final List<Integer> list1 = Arrays.asList(1, 2, 3, 4, 5, 9); final List<Integer> list2 = Arrays.asList(1, 10, 100, 1000, 10000); final List<Integer> list3 = Arrays.asList(6, 7, 8); final List<List<Integer>> listOfLists = Arrays.asList(list1, list2, list3); final List<Integer> allIntegers = concatenate(listOfLists); final int maxList1 = findMax(list1); final int minList2 = findMin(list2); final int maxEmpty = findMax(Collections.emptyList()); final int minEmpty = findMin(Collections.emptyList()); final int maxOrZeroEmpty = findMinOrZero(Collections.emptyList()); final int minOrZeroEmpty = findMaxOrZero(Collections.emptyList()); final int minOfMaxes = findMinOfMaxes(listOfLists); final int minOfMaxesEmpty = findMinOfMaxes(Collections.emptyList()); final int minOfMaxesListOfEmptyLists = findMinOfMaxes(Arrays.asList(Collections.emptyList(), Collections.emptyList()));