Home > database >  Is there anyway to remove/Replace a Column from a list of lists in Java?
Is there anyway to remove/Replace a Column from a list of lists in Java?

Time:11-04

I have a List of Lists filled with strings and I'm trying to sort a column of the list So i could then use binary search on it to find an element within that column. I also want to get all the information on the Row after finding the element in the column and print it out. I'm trying to replace a column in a list of lists with a sorted version of the column.

Example: I'm looking for "345" in the 3rd column using binary search and want to print the entire row after finding "345" in the column.

before sorting 3rd column:

"Test0" "ABC" "123" "A1"
"Test3" "JKL" "901" "A4"
"Test1" "DEF" "345" "A2"        
"Test4" "MNO" "234" "A5"
"Test2" "GHI" "678" "A3"

after sorting 3rd column: 

"Test0" "ABC" "123" "A1"
"Test4" "MNO" "234" "A5"
"Test1" "DEF" "345" "A2"   
"Test2" "GHI" "678" "A3"
"Test3" "JKL" "901" "A4"

Output: 

"Test1" "DEF" "345" "A2"  

I already have a method that can get any column from the list and sort it, I just need to find a way to replace that sorted column back into the list if possible. Then I could run Binary search and print the row(s).

Edit: I've tried making this method that attempts loop through and add the updated column to the original 2d string array then i convert it to a lists of lists. It just ends up returning the original list.

public static List <List<String>> replaceCol (String [][] dataArray, List <String> col, int valType){
        List<List<String>> updatedList = new ArrayList<>();

        for (int i = 0; i < dataArray.length; i  ) {
            for (int j = 0; j < dataArray.length; j  ) {

            }
            if (dataArray[i][valType].equalsIgnoreCase(col.get(i))) {
                dataArray[i][valType] = col.get(i);
            }
        }
        updatedList = TwoDArrayToList(dataArray);

        return updatedList;
    }

CodePudding user response:

For this, I created a method that returns the index of the matching element. If no matching element is found, it simply returns -1.

I also created a comparator that compares lists based on the value of the string at the found index. If the index is less than zero, it simply skips the comparator to avoid an exception being thrown. Since the list is of String objects and this class already implements the Comparator interface, the rest is easy: simply return the result of comparison of the compared strings.

public class GridSort {
    public static void main (String[] args) {
        List<List<String>> grid = new ArrayList<>();
        grid.add(List.of("Test0", "ABC", "123", "A1"));
        grid.add(List.of("Test3", "JKL", "901", "A4"));
        grid.add(List.of("Test1", "DEF", "345", "A2"));
        grid.add(List.of("Test4", "MNO", "234", "A5"));
        grid.add(List.of("Test2", "GHI", "678", "A3"));
        
        String key = "345";
        int pivotPoint = findPivotPoint(grid, key); // returns index 2
        
        // sorting
        Comparator<List<String>> rowComparator = new Comparator<List<String>>() {
            
            @Override
            public int compare (List<String> o1, List<String> o2) {
                String s1 = o1.get(pivotPoint);
                String s2 = o2.get(pivotPoint);
                return s1.compareTo(s2);
            }       
        };
        
        if (pivotPoint >= 0) {
            Collections.sort(grid, rowComparator);
        }
        
        System.out.println("Pivot Point: "   pivotPoint);
        grid.stream().forEach(System.out::println);
    }
    
    private static int findPivotPoint(List<List<String>> grid, String key) {
        for (List<String> list : grid) {
            OptionalInt indexOpt = IntStream.range(0, list.size())
             .filter(i -> key.equals(list.get(i)))
             .findFirst();
            if (indexOpt.isPresent()) {
                return indexOpt.getAsInt();
            }
        }
        return -1;
    }
}

This prints out

Pivot Point: 2
[Test0, ABC, 123, A1]
[Test4, MNO, 234, A5]
[Test1, DEF, 345, A2]
[Test2, GHI, 678, A3]
[Test3, JKL, 901, A4]

If instead you pass "foo" as the key, the original list is printed out:

Pivot Point: -1
[Test0, ABC, 123, A1]
[Test3, JKL, 901, A4]
[Test1, DEF, 345, A2]
[Test4, MNO, 234, A5]
[Test2, GHI, 678, A3]

Just a clarifying note, the List<List<String>> has to be mutable since the contents of the list needs to be changed for sorting. However, since I did not intended to change any of the List<String>, I created those using List.of() to make them immutable. If you attempt to mutate these lists, an exception will be thrown.

  • Related