Question

In: Computer Science

Write a method public static <E> List<List<E>> filterMinLength(List<List<E>> listOfLists, int minLength) that given a list of...

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.

Solutions

Expert Solution

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)


Related Solutions

JAVA Given the header of a method public static void m1 (int[ ] max) Write down...
JAVA Given the header of a method public static void m1 (int[ ] max) Write down Java codes to invoke m1 method, declare variables as needed, (Do NOT implement the method)
Write a method with the following header: public static char calculateLetterGrade(int grade, int totalMarks) This method...
Write a method with the following header: public static char calculateLetterGrade(int grade, int totalMarks) This method calculates grade/totalMarks and returns a letter grade based on the following scale: 0-50 = F 51-60 = D 61-70 = C 71-80 = B 81-100 = A
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an...
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an argument and returns the sum for every other Int from n down to 1. For example, sumForEachOther(8) should return 20, since 8+6+4+ 2=20.And the call sumForEachOther(9) should return 25 since 9+7+5 + 3+1-=25. Your method must use recursion.
Write a method with the following header: public static void showGradeDistribution(int a, int b, int c,...
Write a method with the following header: public static void showGradeDistribution(int a, int b, int c, int d, int f) It should print a graph (using asterisks) for each of the letters entered in the reverse order of the parameter list and with a label. In addition, if A and B grades sum is equal or exceeds that of grades C and D and F, the message “Strong class!” should be displayed. For example a method call of: showGradeDistribution(5,7,4,4,3); Would...
Given the following method declaration, write a valid method call. public static void calcArea(String roomName, int...
Given the following method declaration, write a valid method call. public static void calcArea(String roomName, int length, int width)
In Java, write the method public static void insertUnique(List l, T e), user of the ADT...
In Java, write the method public static void insertUnique(List l, T e), user of the ADT List. The method takes a list l and an element e and inserts the element at the end of the list only if it is not already there. Example 0.2. If l : A → B → C, then after calling insertUnique(l, "C"), the list does not change. Calling insertUnique(l, "D") will make l be : A → B → C → D.
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
Write a method public static void minMax(int[] arr) that takes an array of unique ints of...
Write a method public static void minMax(int[] arr) that takes an array of unique ints of length at least two as an argument, and swaps the smallest value of the array into the 0th position and swaps the largest value of the array into the last position. For example, if int[] a = {4, 3, 2, 6, 1, 5}, the method call minMax(a) should modify the array so that it is {1, 3, 2, 5, 4, 6}. The method should...
rite a method with the following header: public static void showGradeDistribution(int a, int b, int c,...
rite a method with the following header: public static void showGradeDistribution(int a, int b, int c, int d, int f) It should print a graph (using asterisks) for each of the letters entered in the reverse order of the parameter list and with a label. In addition, if A and B grades sum is equal or exceeds that of grades C and D and F, the message “Strong class!” should be displayed. For example a method call of: showGradeDistribution(5,7,4,4,3); Would...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT