In: Computer Science
Write a method public static <E> List<List<E>> filterMinLength(List<List<E>> listOfLists, int minLength) that given a list of lists and a minimum length returns a new list of lists, containing only the lists that are at least as long as the minimum length in the same order they appeared in the input. The original list must not be modified.
For example, if listOfLists = [[1, 2, 3], [], [4, 5], [6, 7, 8, 9]] and minLength = 3, then the method should return a new list containing two lists: [[1, 2, 3], [6, 7, 8, 9]].
For full credit, your method must correctly use generics, and must not contain unnecessary method calls or loops, even if they do not otherwise impact correctness.
You may assume List and ArrayList are correctly imported. You may not import other classes.
import java.util.ArrayList;
import java.util.List;
public class TestMinList {
/*
* returns a new list of lists, containing only the
lists that are at least as
* long as the minimum length in the same order they
appeared in the input.
*/
public static <E> List<List<E>>
filterMinLength(List<List<E>>
listOfLists, int minLength) {
/*create a list to add valid lists
into it and return*/
List<List<E>>
listToReturn = new ArrayList<List<E>>();
/*traverse the passed list to the
method and check for lengths of each list*/
for (List<E> list :
listOfLists) {
/*check if
length is as at least as minimum length passed to method*/
if (list.size()
>= minLength) {
/*add the list to our list to be
returned*/
listToReturn.add(list);
}
}
return listToReturn;//return the
list when formed
}
public static void main(String[]
args) {
/*create lists to be added to
List<List<E>>*/
List<Integer> l1 = new
ArrayList<>();
List<Integer> l2 = new
ArrayList<>();
List<Integer> l3 = new
ArrayList<>();
List<Integer> l4 = new
ArrayList<>();
l1.add(1);l1.add(2);l1.add(3);l3.add(6);l3.add(7);
l3.add(8);l3.add(9);l4.add(4);l4.add(5);
/*once list are created then add to
our final List<List<E>>*/
List<List<Integer>>
list = new ArrayList<List<Integer>>();
list.add(l1);
list.add(l2);
list.add(l4);
list.add(l3);
System.out.println("Initial
List<List<Integer>>: " + list.toString());
List<List<Integer>>
listReturned = filterMinLength(list, 3);
System.out.println("filtered
List<List<Integer>> with minLength as 3: " +
listReturned.toString());
System.out.println("\n\n");
/*additional
List<List<String>> to check the method*/
List<String> s1 = new
ArrayList<>();
List<String> s2 = new
ArrayList<>();
List<String> s3 = new
ArrayList<>();
List<String> s4 = new
ArrayList<>();
s1.add("A");s1.add("B");s1.add("C");s1.add("D");s1.add("E");
s3.add("Z");s3.add("Y");s3.add("X");s3.add("W");s3.add("V");
s4.add("G");s4.add("F");
List<List<String>>
listString = new ArrayList<List<String>>();
listString.add(s1);
listString.add(s2);
listString.add(s4);
listString.add(s3);
System.out.println("Initial
List<List<String>>: " + listString.toString());
List<List<String>>
listStringReturned = filterMinLength(listString, 4);
System.out.println("filtered
List<List<String>> with minLength as 4: " +
listStringReturned.toString());
}
}
screenshhot of the code:
Tested the above method by creating List<List<Integer>> and List<List<String>>.
Output when above code is run will be as below:
**sysout are used to display the correct working..
to format your code Ctrl+a then Ctrl+Shift+f (for Eclipse IDE)