【发布时间】:2015-10-14 18:48:10
【问题描述】:
我需要在 Java 中使用两个文件的设置差异。每个文件大约有 5000 万行,所以我无法将它们完全加载到内存中。我可以完成这些阶段,但我计划使用来自 linux 的 comm 命令,它可以有效地做到这一点。
- java 中是否有一个库可以有效地完成这项工作?
- 从程序中调用 shell 命令是不好的设计吗?
详情
我有 file1 和 file2 每个都有超过 4000 万行。我不想把它们放在记忆中。我需要找到file1 - file2的设置差异。即在 file1 但不在 file2 中的行。一般来说,我会遵循算法:
1. Read file1 line by line and save it in HashSet.
2. Read file2 line by line.
3. Remove each line of file2 from Hashset if present
有什么方法可以在不将 file1 保存在 Hashset 中的情况下做到这一点。
编辑:我的解决方案
我终于决定使用bloom来解决这个目的。我知道布隆过滤器给出了近似的答案,但我已经将位集长度足够长*(14*file1 的大小,即 1000 万)*,这给了我 10^-9 的精度。以下是算法
1. Read each line of file2 and add to Bloom Filter.
2. Now, file2 is compressed from 300MB+ to 40MB+
3. Read each line of file1, if not present in filter print the line
【问题讨论】:
-
没有更多上下文,这个问题是题外话,因为它要求外部库或主要基于意见的答案
-
我正在编辑问题。
标签: java shell bloom-filter