Home > Enterprise >  compare data from 2 files and return the data thats not in either of the files?
compare data from 2 files and return the data thats not in either of the files?

Time:10-30

lets say i have 2 files with ids and dates column

file1:
empid1: date 1
empid1: date 2
empid2: date 3
empid2: date 4

file2:
empid1: date 1
empid2: date 3
empid3: date 5

so, now, when i compare the 2 files, my main goal is to retrieve those data that are not in the file 2 and vice versa. Can anyone help me or guide me on how to achieve this?

CodePudding user response:

public static void main(String[] args) {
    List<String> f = Arrays.asList(new String[] { "A", "B", "D" });
    List<String> s = Arrays.asList(new String[] { "A", "B", "C" });
    System.out.println(Arrays.toString(nonCommonLines(f, s).toArray()));
}

public static List<String> nonCommonLines(List<String> first, List<String> second) {
    List<String> nonCommonReturn = new ArrayList<String>();
    if (first != null && second != null) {
        for (String firstIterator : first) {
            if (Collections.frequency(second, firstIterator) == 0) {
                nonCommonReturn.add(firstIterator);
            }
        }
        for (String secondIterator : second) {
            if (Collections.frequency(first, secondIterator) == 0) {
                nonCommonReturn.add(secondIterator);
            }
        }
    } else {
        if (first == null)
            nonCommonReturn.addAll(second);
        if (second == null)
            nonCommonReturn.addAll(first);
    }
    return nonCommonReturn;
}

CodePudding user response:

7k entries is not a lot. With today's computers having gigabytes of RAM, you should be able to store the entire contents of both files in memory so consider the following code (which is suitable for Java 8)

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class Testings {

    public static void main(String[] args) {
        Path path1 = Paths.get("path-to-first-file");
        Path path2 = Paths.get("path-to-second-file");
        try {
            List<String> lines1 = Files.readAllLines(path1);

            // Make copy of 'lines1'.
            List<String> org1 = new ArrayList<>(lines1);

            List<String> lines2 = Files.readAllLines(path2);

            // Now 'lines1' only contains lines that are not in 'lines2'.
            lines1.removeAll(lines2);

            // Now 'lines2' only contains lines that are not in [original] 'lines1'.
            lines2.removeAll(org1);

            // 'lines1' now contains lines that were not common in the original lists.
            lines1.addAll(lines2);
        }
        catch (IOException x) {
            x.printStackTrace();
        }
    }
}
  • Related