I have printed two array lists from two CSV files
CSV 1 to arraylist
BufferedReader br = new BufferedReader(new InputStreamReader(stream));
String text = br.readLine();
String[] b = text.split(String.valueOf(str));
csv1.add(b[0]);
CSV 2 to arraylist
BufferedReader br = new BufferedReader(new InputStreamReader(stream));
String text = br.readLine();
String[] b = text.split(String.valueOf(str));
csv2.add(b[0]);
I need to compare column names but they are labelled differently in each file. column John in list 1 is equal to col smith in list 2
ArrayList1 = ["John","Jane","Vehicle","Snack","Ring"]
ArrayList2 = ["Car","Bar","Smith","Doe","Ring"]
For example; John in list1 has all the same data as smith in list2. I need to check if John is present in list one, then smith SHOULD be present in list 2 and check for that column name.
CodePudding user response:
The method takes two lists and two supplied strings and checks to see if both strings appear in opposing lists.
public boolean checkName(List<String> list1, List<String>
list2, String one, String two) {
return list1.contains(one) && list2.contains(two) ||
list1.contains(two) && list2.contains(one);
}
CodePudding user response:
You could use a map if you know which column names are linked to each other.
Map<String,String> map = new HashMap<>();
map.put("John", "Smith");
map.put("Jane", "Doe");
map.put("Vehicle", "Car");
map.put("Snack", "Bar");
map.put("Ring", "Ring");
Assuming your lines from your two csv files look something like
String lineFromFirstCsv = "John,Jane,Vehicle,Snack,Ring";
String lineFromSecondCsv = "Car,Bar,Smith,Doe,Ring";
You could for example check each item from first file and print a message:
String[] firstCsvColumns = lineFromFirstCsv.split(",");
String[] secondCsvColumns = lineFromSecondCsv.split(",");
for (String column : firstCsvColumns) {
boolean found = Arrays.stream(secondCsvColumns).anyMatch(map.get(column)::equals);
if (found){
System.out.println(String.format("Found matching columns: %s -> %s.", column, map.get(column)));
}
else {
System.out.println(String.format("No matching column found for: %s.", column));
}
}