# 两个数据量很大的集合求差集的高效方法

9 个回答

#### 方法二

``````@Test
public void test_1010000038638664() {
int size = 1000_000;
List<String> list1 = new ArrayList<>(1000_000);
List<String> list2 = new ArrayList<>(1000_000);

// 准备测试数据
String str = "kdajfoeiajflsajf3ijfalsjfa;lsdlfkjioawjlsdf";

for (int i = 0; i < size; i++) {
if (i % 3 == 0) {
} else {
}
}

// 用Set来过滤和测试性能
final Set<String> set = new HashSet<>(list2);
List<String> result = new ArrayList<>();
long startTime = System.currentTimeMillis();

for (String v : list1) {
if (!set.contains(v)) {
}
}

System.out.println("Took: " + (System.currentTimeMillis() - startTime) + "ms. size: " + result.size());
// Took: 96ms. size: 666666

// 如果还想更快，可以试一下parallel stream
startTime = System.currentTimeMillis();
result = list1.parallelStream().filter(it -> !set.contains(it)).collect(Collectors.toList());

System.out.println("Took: " + (System.currentTimeMillis() - startTime) + "ms. size: " + result.size());
// Took: 64ms. size: 666666
}``````

``````public List<String> complement(List<String> l1, List<String> l2) {
HashSet<String> s2 = new HashSet<>(l2);
l1.forEach(s2::remove);
return new ArrayList<>(s2);
}``````

###### 你尚未登录，登录后可以
• 和开发者交流问题的细节
• 关注并接收问题和回答的更新提醒
• 参与内容的编辑和改进，让解决方法与时俱进