测试的机器为4核4线程,16G内存
public static void main(String[] args) {
long time = System.currentTimeMillis();
List<String> list = new ArrayList();
for (int i = 0; i < 1000000; i++) {
int start=10000000;
int end=99999999;
int number = new Random().nextInt(end - start + 1) + start;
list.add(String.valueOf(number));
}
long time1 = System.currentTimeMillis();
System.out.println("第一次处理耗时:"+(time1 - time));
List<String> list2 = new ArrayList();
for (int i = 0; i < 10000000; i++) {
int start=10000000;
int end=99999999;
int number = new Random().nextInt(end - start + 1) + start;
list2.add(String.valueOf(number));
}
long time2 = System.currentTimeMillis();
System.out.println("第二次处理耗时:"+(time2 - time1));
//最后结果集
List<String> resultList = new ArrayList<>();
//中间存储
Map<String, String> map = new HashMap<>();
list2.forEach(i2 -> {
map.put(i2 + "", i2);
});
list.forEach(i1 -> {
String m = map.get(i1 + "");
//如果为空,则证明list2中无该数据
if (m == null) {
resultList.add(i1);
}
});
long time3 = System.currentTimeMillis();
System.out.println("第三次处理耗时:"+(time3 - time2));
System.out.println("差集的数量为:"+resultList.size());
}
- THE END -
最后修改:2023年5月24日