Home > Blockchain >  Comparing two CSV files having line by line and print missing id
Comparing two CSV files having line by line and print missing id

Time:08-05

CSV 1
______                    
  UUID                               ID
9da0dec2-1f66-4c5b-a859-a07c3bb26705,3101659035262004
04977348-d0a3-4836-8bbc-d46cb3c4f962,3101659036985015
e63440bb-d479-4124-b6a8-2b7335dd5e08,3101659036995887
34f0468e-4685-4637-a455-1814e0813639,3101659037151614
42b06952-13f8-4159-ba02-244ea48ca96d,3101659037418477
0581ee59-f811-4b10-86de-f75a04189c0a,3101659037565062
880201c2-db8f-4859-8fdf-1d398566b4fc,3101659037610684
35a37636-fdb2-40c4-a2c0-5e95dded2d1e,3101659037902182
280907c1-39a2-4b77-8613-66cb739f140d,3101659038153773

csv2

ID
3101659035262004
3101659036995887
3101659037151614
3101659037418477
3101659037565062
3101659037610684
3101659037902182
3101659038153773

Need to compare CSV1 and CSV2 on the basis of CSV1(ID) and CSV2(ID) and print missing line. all the values in ID are already sorted . I need to get missing UUID against the missing ID in CSV1. I am new to java and trying to solve this but finding it difficult. any help appreciated.

EDIT: As per the comments by fellow developers i tried following approach which is still very rough, would like to know the improvments i can make to it

BufferedReader br = new BufferedReader(new FileReader("/Users/IdeaProjects/extract/csv1.csv"));
def line =  null;
HashMap<String,String> map = new HashMap<String, String>();

while((line=br.readLine())!=null){
    def str = line.split(",");
    for(int i=0;i<str.length;i  ){
     //   def arr = str[i].split(",");
        map.put(str[1], str[0]);
    }
}
//System.out.println(map);

BufferedReader br2 = new BufferedReader(new FileReader("/Users/IdeaProjects/extract/csv2.csv"));

while((line2=br2.readLine())!=null){
    def str2 = line2.split(",");
    for(int i=0;i<str2.length;i  ){
        //   def arr = str[i].split(",");
        map.remove(str2[0])
    }
}

CodePudding user response:

There are many ways to solve your problem. I will explain the way I would do it, it is quiet simple and doesn't need "much" know-how.

So at first you should read both CSV files into Java and store them somewhere. I recommend an ArrayList. It is easy to use and based on the size of your data, the datastructure really does not matter.

So reading a CSV file could look like this:

ArrayList<String> ids = new ArrayList<String>();
    
try {
    BufferedReader reader = new BufferedReader(new FileReader(filePath));
    
    String line = "";
    while((line = reader.readLine()) != null) {
        ids.add(line)
    }
} 
catch (Exception e) {
    System.err.println("The given input file does not exist, probably wrong  file path!");
}

After you did read both .csv files into two different ArrayLists, you store them both and should write another method like "findMissingIDs()".

In this method you should take the ArrayList, that stores all existing ID's, iterate over that and call ArrayList.contains(ArrayList.get(i)). The ArrayList that calls .contains() should be the ArrayList, that does not contain all ID's, and the ArrayList that calls .get() should be the ArrayList containing all ID's

All in all it could look like this:

public void findMissingIDs() {
    ArrayList<String> allIDs = new ArrayList<>();
    ArrayList<String> toCompare = new ArrayList<>();
    
    for (int i = 0; i < allIDs.size(); i  ) {
        boolean contains = false;
        
        if (toCompare.contains(allIDs.get(i))) {
            contains = true;
        }
        else {
            contains = false;
        }
        
        // DO SOMETHING BASED ON "contains".
    }
}

The two ArrayLists should be replaced by the ArrayList holding the information from the .csv file of course

  • Related