如题:有List<Stringlist1和List<Stringlist2,两个集合各有上万个元素,怎样取出两个集合中不同的元素?

方法1:遍历两个集合

 public static void main(String[] args) {
List<String> list1 = new ArrayList<String>();
List<String> list2 = new ArrayList<String>(); for(int i = 0; i < 10000; i++){
list1.add("test" + i);
list2.add("test" + i*2);
}
getDifferent(list1, list2);
getDiffrent2(list1, list2);
getDiffrent3(list1, list2);
getDiffrent4(list1, list2);
} private static List<String> getDifferent(List<String> list1, List<String> list2){
long startTime = System.currentTimeMillis();
List<String> diff = new ArrayList<String>();
for(String str : list1){
if(!list2.contains(str)){
diff.add(str);
}
}
System.out.println("Total Time: " + (System.currentTimeMillis() - startTime));
return diff;
}
千万不要采用这种方法,总共要循环的次数是两个List的size相乘的积,从输出看耗时也是比较长的,那么我们有没有其他的方法呢?当然有.

方法2:采用List提供的retainAll()方法:
 public static void main(String[] args) {
List<String> list1 = new ArrayList<String>();
List<String> list2 = new ArrayList<String>(); for (int i = 0; i < 10000; i++) {
list1.add("test" + i);
list2.add("test" + i * 2);
}
getDifferent(list1, list2);
} private static List<String> getDiffrent2(List<String> list1, List<String> list2) {
long startTime = System.currentTimeMillis();
list1.retainAll(list2);
System.out.println("Total Time: " + (System.currentTimeMillis() - startTime));
return list1;
}

很遗憾,这种方式虽然只要几行代码就搞定,但是这个却更耗时,查看retainAll()的源码:

 public boolean retainAll(Collection<?> c) {
boolean modified = false;
Iterator<E> e = iterator();
while (e.hasNext()) {
if (!c.contains(e.next())) {
e.remove();
modified = true;
}
}
return modified;
}

无需解释这个耗时是必然的,那么我们还有没有更好的办法呢?仔细分析以上两个方法中我都做了mXn次循环,其实完全没有必要循环这么多次,我们的需求是找出两个List中的不同元素,那么我可以这样考虑:用一个map存放lsit的所有元素,其中的key为lsit1的各个元素,value为该元素出现的次数,接着把list2的所有元素也放到map里,如果已经存在则value加1,最后我们只要取出map里value为1的元素即可,这样我们只需循环m+n次,大大减少了循环的次数。

     private static List<String> getDiffrent3(List<String> list1, List<String> list2) {
long startTime = System.currentTimeMillis();
Map<String, Integer> map = new HashMap<String, Integer>(list1.size() + list2.size());
List<String> diff = new ArrayList<String>();
for (String string : list1) {
map.put(string, 1);
}
for (String string : list2) {
Integer cc = map.get(string);
if (cc != null) {
map.put(string, ++cc);
continue;
}
map.put(string, 1);
}
for (Map.Entry<String, Integer> entry : map.entrySet()) {
if (entry.getValue() == 1) {
diff.add(entry.getKey());
}
}
System.out.println("Total Time: " + (System.currentTimeMillis() - startTime));
return list1;
}
显然,这种方法大大减少耗时,是方法1的1/4,是方法2的1/40,这个性能的提升时相当可观的,但是,这不是最佳的解决方法,观察方法3我们只是随机取了一个list作为首次添加的标准,这样一旦我们的list2比list1的size大,则我们第二次put时的if判断也会耗时,做如下改进:
 private static List<String> getDiffrent4(List<String> list1, List<String> list2) {
long st = System.nanoTime();
Map<String,Integer> map = new HashMap<String,Integer>(list1.size()+list2.size());
List<String> diff = new ArrayList<String>();
List<String> maxList = list1;
List<String> minList = list2;
if(list2.size()>list1.size())
{
maxList = list2;
minList = list1;
}
for (String string : maxList) {
map.put(string, 1);
}
for (String string : minList) {
Integer cc = map.get(string);
if(cc!=null)
{
map.put(string, ++cc);
continue;
}
map.put(string, 1);
}
for(Map.Entry<String, Integer> entry:map.entrySet())
{
if(entry.getValue()==1)
{
diff.add(entry.getKey());
}
}
System.out.println("getDiffrent4 total times "+(System.nanoTime()-st));
return diff; }

这里对连个list的大小进行了判断,小的在最后添加,这样会减少循环里的判断,性能又有了一定的提升,正如一位朋友所说,编程是无止境的,只要你认真去思考了,总会找到更好的方法!
非常感谢binglian的指正,针对List有重复元素的问题,做以下修正,首先明确一点,两个List不管有多少个重复,只要重复的元素在两个List都能找到,则不应该包含在返回值里面,所以在做第二次循环时,这样判断:如果当前元素在map中找不到,则肯定需要添加到返回值中,如果能找到则value++,遍历完之后diff里面已经包含了只在list2里而没在list2里的元素,剩下的工作就是找到list1里有list2里没有的元素,遍历map取value为1的即可:

 private static List<String> getDiffrent5(List<String> list1, List<String> list2) {
long st = System.nanoTime();
List<String> diff = new ArrayList<String>();
List<String> maxList = list1;
List<String> minList = list2;
if(list2.size()>list1.size())
{
maxList = list2;
minList = list1;
}
Map<String,Integer> map = new HashMap<String,Integer>(maxList.size());
for (String string : maxList) {
map.put(string, 1);
}
for (String string : minList) {
if(map.get(string)!=null)
{
map.put(string, 2);
continue;
}
diff.add(string);
}
for(Map.Entry<String, Integer> entry:map.entrySet())
{
if(entry.getValue()==1)
{
diff.add(entry.getKey());
}
}
System.out.println("getDiffrent5 total times "+(System.nanoTime()-st));
return diff; }
												

Java Collection - 003 高效的找出两个List中的不同元素的更多相关文章

  1. Java - Collection 高效的找出两个List中的不同元素

    如题:有List<String> list1和List<String> list2,两个集合各有上万个元素,怎样取出两个集合中不同的元素? 方法1:遍历两个集合 public ...

  2. 高效的找出两个List中的不同元素

    /* * TestList.java * Version 1.0.0 * Created on 2017年12月15日 * Copyright ReYo.Cn */ package reyo.sdk. ...

  3. python——快速找出两个电子表中数据的差异

    最近刚接触python,找点小任务来练练手,希望自己在实践中不断的锻炼自己解决问题的能力. 公司里会有这样的场景:有一张电子表格的内容由两三个部门或者更多的部门用到,这些员工会在维护这些表格中不定期的 ...

  4. python:找出两个列表中相同和不同的元素(使用推导式)

    #接口返回值 list1 = ['张三', '李四', '王五', '老二'] #数据库返回值 list2 = ['张三', '李四', '老二', '王七'] a = [x for x in lis ...

  5. 使用Eclipse在Excel中找出两张表中相同证件号而姓名或工号却出现不同的的项

    1:首先把Excel中的文本复制到txt中,复制如下: A表: 证件号                           工号  姓名 310110xxxx220130004 101 傅家宜3101 ...

  6. 389. Find the Difference 找出两个字符串中多余的一个字符

    [抄题]: Given two strings s and t which consist of only lowercase letters. String t is generated by ra ...

  7. C语言:对传入sp的字符进行统计,三组两个相连字母“ea”"ou""iu"出现的次数,并将统计结果存入ct所指的数组中。-在数组中找出最小值,并与第一个元素交换位置。

    //对传入sp的字符进行统计,三组两个相连字母“ea”"ou""iu"出现的次数,并将统计结果存入ct所指的数组中. #include <stdio.h& ...

  8. 找出此产品描述中包含N个关键字的长度最短的子串

    阿里巴巴笔试题:给定一段产品的英文描述,包含M个英文字母,每个英文单词以空格分隔,无其他标点符号:再给定N个英文关键词,请说明思路并变成实现方法. String extractSummary(Stri ...

  9. FCC JS基础算法题(5):Return Largest Numbers in Arrays(找出多个数组中的最大数)

    题目描述: 找出多个数组中的最大数右边大数组中包含了4个小数组,分别找到每个小数组中的最大值,然后把它们串联起来,形成一个新数组.提示:你可以用for循环来迭代数组,并通过arr[i]的方式来访问数组 ...

随机推荐

  1. ecmall 开发一个新模块

    要开发新模块,要借鉴原有模块的代码并进行修改. 首先打开目录external/modules 会有一个datacall文件夹,这个文件夹对应的就是一个模块. 复制datacall文件夹,重命名为tes ...

  2. 【转载】 如何看待 2019 年 CS PhD 现扎堆申请且大部分为 AI 方向?未来几年 AI 泡沫会破裂吗?

    原贴地址: https://www.zhihu.com/question/316135639 作为一个 AI 方向的在读博士生,实在是过的蛮闹心,无意中逛知乎发现了这个帖子,发现很适合现在的自己,于是 ...

  3. tmux学习

    1.基本命令: http://blog.chinaunix.net/uid-26285146-id-3252286.html (重要) http://blog.csdn.net/longxibendi ...

  4. Django之模板层-自定义过滤器以及标签

    自定义标签与过滤器 在settings中的INSTALLED_APPS配置当前app,不然django无法找到自定义的simple_tag. 在app中创建templatetags模块(模块名只能是t ...

  5. python调用caffe环境配置

    背景是这样的,项目需要,必须将训练的模型通过C++进行调用,所以必须使用caffe或者mxnet,而caffe是用C++实现,所以有时候简单的加载一张图片然后再进行预测十分不方便 用caffe写pro ...

  6. [LeetCode&Python] Problem 226. Invert Binary Tree

    Invert a binary tree. Example: Input: 4 / \ 2 7 / \ / \ 1 3 6 9 Output: 4 / \ 7 2 / \ / \ 9 6 3 1 Tr ...

  7. 洛谷P4147 玉蟾宫(动规:最大子矩形问题/悬线法)

    题目链接:传送门 题目大意: 求由F构成的最大子矩阵的面积.输出面积的三倍. 1 ≤ N,M ≤ 1000. 思路: 悬线法模板题. #include <bits/stdc++.h> us ...

  8. Javascript中的Bind,Call和Apply

    http://www.html-js.com/article/JavaScript-functional-programming-in-Javascript-Bind-Call-and-Apply?s ...

  9. PTA——天平找小球

    PTA 7-22 用天平找小球 #include<stdio.h> int main() { int a,b,c; scanf("%d%d%d",&a,& ...

  10. YIT-CTF—Web

    一:背后 打开传送门——>查看网页源代码——>1b0679be72ad976ad5d491ad57a5eec0——>用MD5解密 二:一种编码 [][(![]+[])[+[]]+([ ...