我的程序在内存中生成两个集合,我不想将它们写到文件中。每一组都有一组线。每一行都是一系列数字。我想检查这两个文件在java中是否有相同的行集(java是必须的),高效地。
例如:
set1 :
1 2 3
3 4 5 6
1 2 4 5 7
8
7
1 2 4
set2 :
7
3 4 5 6
1 2 4 5 7
1 2 3
8
1 2 4 因此,在set1和set2的情况下,这组行是相同的,它们只是混乱了。因此,对于这两个集合作为输入,我在java中的算法应该返回true。你能在这方面帮助我吗?我使用哈希表实现了它,但是它用处不大。
发布于 2014-03-21 04:30:02
如果你的行是字符串,你可以这样做
List<String> a = new ArrayList<>(Arrays.asList(new String[] { "1 2 3",
"3 4 5 6", "1 2 4 5 7", "8", "7", "1 2 4" }));
List<String> b = new ArrayList<>(Arrays.asList(new String[] { "7",
"3 4 5 6", "1 2 4 5 7", "1 2 3", "8", "1 2 4" }));
if (a.size() == b.size()) {
a.removeAll(b);
if (a.isEmpty())
System.out.println("A and B are the same sets");
}https://stackoverflow.com/questions/22544154
复制相似问题