【问题标题】:compare data from 2 files and return the data thats not in either of the files?比较来自 2 个文件的数据并返回不在这两个文件中的数据?
【发布时间】:2021-10-29 14:28:12
【问题描述】:

假设我有 2 个带有 id 和日期列的文件

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

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

所以,现在,当我比较这 2 个文件时,我的主要目标是检索那些不在文件 2 中的数据,反之亦然。 谁能帮助我或指导我如何实现这一目标?

【问题讨论】:

  • 到目前为止您尝试过什么?向我们展示您的代码,有什么问题?
  • 你的意思是只需要输出两个文件不共有的行吗?
  • 是的,输出不常见的。
  • 读取文件1,将行存储在一个列表中-> file1list 维护一个单独的列表->uncommon 逐行读取文件2,如果该行在file1list中不存在,则将其放入uncommon return如果你能展示你迄今为止所做的事情和你面临的问题,那就太好了
  • 文件不需要按排序顺序,文件超过 7k 条目,我正在使用 1.8

标签: java csv file oop structure


【解决方案1】:

7k 条目并不多。今天的计算机拥有千兆字节的 RAM,您应该能够将两个文件的全部内容存储在内存中,因此请考虑以下代码(适用于 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();
        }
    }
}

【讨论】:

    【解决方案2】:
    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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-09
      • 1970-01-01
      • 1970-01-01
      • 2021-07-12
      • 2021-08-30
      • 1970-01-01
      相关资源
      最近更新 更多