Home > Mobile >  Validate columns in a csv file are present in another csv file JAVA
Validate columns in a csv file are present in another csv file JAVA

Time:02-08

I have two arrays ArrayList<String> csv1 and ArrayList<String> csv2

csv2 has more columns than csv1 but the goal is to check that all columns in csv1 are present in csv2, regardless of the extra files.

I esstenially want the code to only look at the columns in csv1 and check if they are in csv2 and nothing else.

Currently its giving me a 'false' output because there are extra columns in csv2 so its saying that they do not match.

I have a class called ReadCsvFile that reads csv files from a path

Public static void main(String[] args) {
ReadCsvFile readFile - new ReadCsvFile();
ArrayList<String> csv1 = (ArrayList<String>) readFile.readcsv1(some parameters here);
ArrayList<String> csv2 = (ArrayList<String>) readFile.readcsv2(some parameters here);
Collections.sort(csv1);
Collections.sort(csv2);
System.out.println(csv1.equals(csv2)); ```
 

CodePudding user response:

You want to know if csv2 containsAll of csv1? Have you looked at the available Methods that Collection or List offers?

CodePudding user response:

The line csv1.equals(csv2) would mean that both file contents are strictly identical.


What you want is :

  • check only header line : use List.get(0) and split on the good separator
  • check if columns of 1 are all in 2 : use List.containsAll
// use your separator if not comma
List<String> header1 = Arrays.asList(csv1.get(0).split(","));
List<String> header2 = Arrays.asList(csv2.get(0).split(","));

boolean ok = header2.containsAll(header1);
System.out.println(ok);
  •  Tags:  
  • Related