转自同名博文,未知真正出处,望作者见谅

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

方法1:遍历两个集合:

  1. package com.czp.test;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. public class TestList {
  5. public static void main(String[] args) {
  6. List<String> list1 = new ArrayList<String>();
  7. List<String> list2 = new ArrayList<String>();
  8. for (int i = 0; i < 10000; i++) {
  9. list1.add("test"+i);
  10. list2.add("test"+i*2);
  11. }
  12. getDiffrent(list1,list2);
  13. //输出:total times 2566454675
  14. }
  15. /**
  16. * 获取两个List的不同元素
  17. * @param list1
  18. * @param list2
  19. * @return
  20. */
  21. private static List<String> getDiffrent(List<String> list1, List<String> list2) {
  22. long st = System.nanoTime();
  23. List<String> diff = new ArrayList<String>();
  24. for(String str:list1)
  25. {
  26. if(!list2.contains(str))
  27. {
  28. diff.add(str);
  29. }
  30. }
  31. System.out.println("total times "+(System.nanoTime()-st));
  32. return diff;
  33. }
  34. }
千万不要采用这种方法,总共要循环的次数是两个List的size相乘的积,从输出看耗时也是比较长的,那么我们有没有其他的方法呢?当然有.

方法2:采用List提供的retainAll()方法:

  1. package com.czp.test;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. public class TestList {
  5. public static void main(String[] args) {
  6. List<String> list1 = new ArrayList<String>();
  7. List<String> list2 = new ArrayList<String>();
  8. for (int i = 0; i < 10000; i++) {
  9. list1.add("test"+i);
  10. list2.add("test"+i*2);
  11. }
  12. getDiffrent(list1,list2);
  13. //输出:total times 2566454675
  14. getDiffrent2(list1,list2);
  15. //输出:getDiffrent2 total times 2787800964
  16. }
  17. /**
  18. * 获取连个List的不同元素
  19. * @param list1
  20. * @param list2
  21. * @return
  22. */
  23. private static List<String> getDiffrent2(List<String> list1, List<String> list2) {
  24. long st = System.nanoTime();
  25. list1.retainAll(list2);
  26. System.out.println("getDiffrent2 total times "+(System.nanoTime()-st));
  27. return list1;
  28. }
  29. /**
  30. * 获取两个List的不同元素
  31. * @param list1
  32. * @param list2
  33. * @return
  34. */
  35. private static List<String> getDiffrent(List<String> list1, List<String> list2) {
  36. long st = System.nanoTime();
  37. List<String> diff = new ArrayList<String>();
  38. for(String str:list1)
  39. {
  40. if(!list2.contains(str))
  41. {
  42. diff.add(str);
  43. }
  44. }
  45. System.out.println("getDiffrent total times "+(System.nanoTime()-st));
  46. return diff;
  47. }
  48. }
  49. 很遗憾,这种方式虽然只要几行代码就搞定,但是这个却更耗时,查看retainAll()的源码:
  50. public boolean retainAll(Collection<?> c) {
  51. boolean modified = false;
  52. Iterator<E> e = iterator();
  53. while (e.hasNext()) {
  54. if (!c.contains(e.next())) {
  55. e.remove();
  56. modified = true;
  57. }
  58. }
  59. return modified;
  60. }
无需解释这个耗时是必然的,那么我们还有没有更好的办法呢?仔细分析以上两个方法中我都做了mXn次循环,其实完全没有必要循环这么多次,我们的需求是找出两个List中的不同元素,那么我可以这样考虑:用一个map存放lsit的所有元素,其中的key为lsit1的各个元素,value为该元素出现的次数,接着把list2的所有元素也放到map里,如果已经存在则value加1,最后我们只要取出map里value为1的元素即可,这样我们只需循环m+n次,大大减少了循环的次数。
  1. package com.czp.test;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. public class TestList {
  7. public static void main(String[] args) {
  8. List<String> list1 = new ArrayList<String>();
  9. List<String> list2 = new ArrayList<String>();
  10. for (int i = 0; i < 10000; i++) {
  11. list1.add("test"+i);
  12. list2.add("test"+i*2);
  13. }
  14. getDiffrent(list1,list2);
  15. //输出:total times 2566454675
  16. getDiffrent2(list1,list2);
  17. //输出:getDiffrent2 total times 2787800964
  18. getDiffrent3(list1,list2);
  19. //输出:getDiffrent3 total times 61763995
  20. }
  21. /**
  22. * 获取两个List的不同元素
  23. * @param list1
  24. * @param list2
  25. * @return
  26. */
  27. private static List<String> getDiffrent3(List<String> list1, List<String> list2) {
  28. long st = System.nanoTime();
  29. Map<String,Integer> map = new HashMap<String,Integer>(list1.size()+list2.size());
  30. List<String> diff = new ArrayList<String>();
  31. for (String string : list1) {
  32. map.put(string, 1);
  33. }
  34. for (String string : list2) {
  35. Integer cc = map.get(string);
  36. if(cc!=null)
  37. {
  38. map.put(string, ++cc);
  39. continue;
  40. }
  41. map.put(string, 1);
  42. }
  43. for(Map.Entry<String, Integer> entry:map.entrySet())
  44. {
  45. if(entry.getValue()==1)
  46. {
  47. diff.add(entry.getKey());
  48. }
  49. }
  50. System.out.println("getDiffrent3 total times "+(System.nanoTime()-st));
  51. return list1;
  52. }
  53. /**
  54. * 获取两个List的不同元素
  55. * @param list1
  56. * @param list2
  57. * @return
  58. */
  59. private static List<String> getDiffrent2(List<String> list1, List<String> list2) {
  60. long st = System.nanoTime();
  61. list1.retainAll(list2);
  62. System.out.println("getDiffrent2 total times "+(System.nanoTime()-st));
  63. return list1;
  64. }
  65. /**
  66. * 获取两个List的不同元素
  67. * @param list1
  68. * @param list2
  69. * @return
  70. */
  71. private static List<String> getDiffrent(List<String> list1, List<String> list2) {
  72. long st = System.nanoTime();
  73. List<String> diff = new ArrayList<String>();
  74. for(String str:list1)
  75. {
  76. if(!list2.contains(str))
  77. {
  78. diff.add(str);
  79. }
  80. }
  81. System.out.println("getDiffrent total times "+(System.nanoTime()-st));
  82. return diff;
  83. }
  84. }
显然,这种方法大大减少耗时,是方法1的1/4,是方法2的1/40,这个性能的提升时相当可观的,但是,这不是最佳的解决方法,观察方法3我们只是随机取了一个list作为首次添加的标准,这样一旦我们的list2比list1的size大,则我们第二次put时的if判断也会耗时,做如下改进:
  1. package com.czp.test;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. public class TestList {
  7. public static void main(String[] args) {
  8. List<String> list1 = new ArrayList<String>();
  9. List<String> list2 = new ArrayList<String>();
  10. for (int i = 0; i < 10000; i++) {
  11. list1.add("test"+i);
  12. list2.add("test"+i*2);
  13. }
  14. getDiffrent(list1,list2);
  15. getDiffrent2(list1,list2);
  16. getDiffrent3(list1,list2);
  17. getDiffrent4(list1,list2);
  18. //        getDiffrent total times 2789492240
  19. //        getDiffrent2 total times 3324502695
  20. //        getDiffrent3 total times 24710682
  21. //        getDiffrent4 total times 15627685
  22. }
  23. /**
  24. * 获取两个List的不同元素
  25. * @param list1
  26. * @param list2
  27. * @return
  28. */
  29. private static List<String> getDiffrent4(List<String> list1, List<String> list2) {
  30. long st = System.nanoTime();
  31. Map<String,Integer> map = new HashMap<String,Integer>(list1.size()+list2.size());
  32. List<String> diff = new ArrayList<String>();
  33. List<String> maxList = list1;
  34. List<String> minList = list2;
  35. if(list2.size()>list1.size())
  36. {
  37. maxList = list2;
  38. minList = list1;
  39. }
  40. for (String string : maxList) {
  41. map.put(string, 1);
  42. }
  43. for (String string : minList) {
  44. Integer cc = map.get(string);
  45. if(cc!=null)
  46. {
  47. map.put(string, ++cc);
  48. continue;
  49. }
  50. map.put(string, 1);
  51. }
  52. for(Map.Entry<String, Integer> entry:map.entrySet())
  53. {
  54. if(entry.getValue()==1)
  55. {
  56. diff.add(entry.getKey());
  57. }
  58. }
  59. System.out.println("getDiffrent4 total times "+(System.nanoTime()-st));
  60. return diff;
  61. }
  62. /**
  63. * 获取两个List的不同元素
  64. * @param list1
  65. * @param list2
  66. * @return
  67. */
  68. private static List<String> getDiffrent3(List<String> list1, List<String> list2) {
  69. long st = System.nanoTime();
  70. Map<String,Integer> map = new HashMap<String,Integer>(list1.size()+list2.size());
  71. List<String> diff = new ArrayList<String>();
  72. for (String string : list1) {
  73. map.put(string, 1);
  74. }
  75. for (String string : list2) {
  76. Integer cc = map.get(string);
  77. if(cc!=null)
  78. {
  79. map.put(string, ++cc);
  80. continue;
  81. }
  82. map.put(string, 1);
  83. }
  84. for(Map.Entry<String, Integer> entry:map.entrySet())
  85. {
  86. if(entry.getValue()==1)
  87. {
  88. diff.add(entry.getKey());
  89. }
  90. }
  91. System.out.println("getDiffrent3 total times "+(System.nanoTime()-st));
  92. return diff;
  93. }
  94. /**
  95. * 获取连个List的不同元素
  96. * @param list1
  97. * @param list2
  98. * @return
  99. */
  100. private static List<String> getDiffrent2(List<String> list1, List<String> list2) {
  101. long st = System.nanoTime();
  102. list1.retainAll(list2);
  103. System.out.println("getDiffrent2 total times "+(System.nanoTime()-st));
  104. return list1;
  105. }
  106. /**
  107. * 获取两个List的不同元素
  108. * @param list1
  109. * @param list2
  110. * @return
  111. */
  112. private static List<String> getDiffrent(List<String> list1, List<String> list2) {
  113. long st = System.nanoTime();
  114. List<String> diff = new ArrayList<String>();
  115. for(String str:list1)
  116. {
  117. if(!list2.contains(str))
  118. {
  119. diff.add(str);
  120. }
  121. }
  122. System.out.println("getDiffrent total times "+(System.nanoTime()-st));
  123. return diff;
  124. }
  125. }
这里对连个list的大小进行了判断,小的在最后添加,这样会减少循环里的判断,性能又有了一定的提升,正如一位朋友所说,编程是无止境的,只要你认真去思考了,总会找到更好的方法!

非常感谢binglian的指正,针对List有重复元素的问题,做以下修正,首先明确一点,两个List不管有多少个重复,只要重复的元素在两个List都能找到,则不应该包含在返回值里面,所以在做第二次循环时,这样判断:如果当前元素在map中找不到,则肯定需要添加到返回值中,如果能找到则value++,遍历完之后diff里面已经包含了只在list2里而没在list2里的元素,剩下的工作就是找到list1里有list2里没有的元素,遍历map取value为1的即可:

  1. package com.czp.test;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. public class TestList {
  7. public static void main(String[] args) {
  8. List<String> list1 = new ArrayList<String>();
  9. List<String> list2 = new ArrayList<String>();
  10. for (int i = 0; i < 10000; i++) {
  11. list1.add("test"+i);
  12. list2.add("test"+i*2);
  13. }
  14. getDiffrent(list1,list2);
  15. getDiffrent3(list1,list2);
  16. getDiffrent5(list1,list2);
  17. getDiffrent4(list1,list2);
  18. getDiffrent2(list1,list2);
  19. //        getDiffrent3 total times 32271699
  20. //        getDiffrent5 total times 12239545
  21. //        getDiffrent4 total times 16786491
  22. //        getDiffrent2 total times 2438731459
  23. }
  24. /**
  25. * 获取两个List的不同元素
  26. * @param list1
  27. * @param list2
  28. * @return
  29. */
  30. private static List<String> getDiffrent5(List<String> list1, List<String> list2) {
  31. long st = System.nanoTime();
  32. List<String> diff = new ArrayList<String>();
  33. List<String> maxList = list1;
  34. List<String> minList = list2;
  35. if(list2.size()>list1.size())
  36. {
  37. maxList = list2;
  38. minList = list1;
  39. }
  40. Map<String,Integer> map = new HashMap<String,Integer>(maxList.size());
  41. for (String string : maxList) {
  42. map.put(string, 1);
  43. }
  44. for (String string : minList) {
  45. if(map.get(string)!=null)
  46. {
  47. map.put(string, 2);
  48. continue;
  49. }
  50. diff.add(string);
  51. }
  52. for(Map.Entry<String, Integer> entry:map.entrySet())
  53. {
  54. if(entry.getValue()==1)
  55. {
  56. diff.add(entry.getKey());
  57. }
  58. }
  59. System.out.println("getDiffrent5 total times "+(System.nanoTime()-st));
  60. return diff;
  61. }
  62. /**
  63. * 获取两个List的不同元素
  64. * @param list1
  65. * @param list2
  66. * @return
  67. */
  68. private static List<String> getDiffrent4(List<String> list1, List<String> list2) {
  69. long st = System.nanoTime();
  70. Map<String,Integer> map = new HashMap<String,Integer>(list1.size()+list2.size());
  71. List<String> diff = new ArrayList<String>();
  72. List<String> maxList = list1;
  73. List<String> minList = list2;
  74. if(list2.size()>list1.size())
  75. {
  76. maxList = list2;
  77. minList = list1;
  78. }
  79. for (String string : maxList) {
  80. map.put(string, 1);
  81. }
  82. for (String string : minList) {
  83. Integer cc = map.get(string);
  84. if(cc!=null)
  85. {
  86. map.put(string, ++cc);
  87. continue;
  88. }
  89. map.put(string, 1);
  90. }
  91. for(Map.Entry<String, Integer> entry:map.entrySet())
  92. {
  93. if(entry.getValue()==1)
  94. {
  95. diff.add(entry.getKey());
  96. }
  97. }
  98. System.out.println("getDiffrent4 total times "+(System.nanoTime()-st));
  99. return diff;
  100. }
  101. /**
  102. * 获取两个List的不同元素
  103. * @param list1
  104. * @param list2
  105. * @return
  106. */
  107. private static List<String> getDiffrent3(List<String> list1, List<String> list2) {
  108. long st = System.nanoTime();
  109. Map<String,Integer> map = new HashMap<String,Integer>(list1.size()+list2.size());
  110. List<String> diff = new ArrayList<String>();
  111. for (String string : list1) {
  112. map.put(string, 1);
  113. }
  114. for (String string : list2) {
  115. Integer cc = map.get(string);
  116. if(cc!=null)
  117. {
  118. map.put(string, ++cc);
  119. continue;
  120. }
  121. map.put(string, 1);
  122. }
  123. for(Map.Entry<String, Integer> entry:map.entrySet())
  124. {
  125. if(entry.getValue()==1)
  126. {
  127. diff.add(entry.getKey());
  128. }
  129. }
  130. System.out.println("getDiffrent3 total times "+(System.nanoTime()-st));
  131. return diff;
  132. }
  133. /**
  134. * 获取连个List的不同元素
  135. * @param list1
  136. * @param list2
  137. * @return
  138. */
  139. private static List<String> getDiffrent2(List<String> list1, List<String> list2) {
  140. long st = System.nanoTime();
  141. list1.retainAll(list2);
  142. System.out.println("getDiffrent2 total times "+(System.nanoTime()-st));
  143. return list1;
  144. }
  145. /**
  146. * 获取两个List的不同元素
  147. * @param list1
  148. * @param list2
  149. * @return
  150. */
  151. private static List<String> getDiffrent(List<String> list1, List<String> list2) {
  152. long st = System.nanoTime();
  153. List<String> diff = new ArrayList<String>();
  154. for(String str:list1)
  155. {
  156. if(!list2.contains(str))
  157. {
  158. diff.add(str);
  159. }
  160. }
  161. System.out.println("getDiffrent total times "+(System.nanoTime()-st));
  162. return diff;
  163. }
  164. }

两个list取不同值的更多相关文章

  1. vue中过滤器比较两个数组取相同值

    在vue中需要比较两个数组取相同值 一个大数组一个 小数组,小数组是大数组的一部分取相同ID的不同name值 有两种写法,两个for循环和map写法 const toName = (ids, arr) ...

  2. jsp取addFlashAttribute值深入理解即springMVC发redirect传隐藏参数

    结论:两种方式 a.如果没有进行action转发,在页面中el需要${sessionScope['org.springframework.web.servlet.support.SessionFlas ...

  3. hdu 5265 技巧题 O(nlogn)求n个数中两数相加取模的最大值

    pog loves szh II Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  4. UVA 10859 - Placing Lampposts 树形DP、取双优值

                              Placing Lampposts As a part of the mission ‘Beautification of Dhaka City’, ...

  5. go 两个数组取并集

    实际生产中,对不同数组取交集.并集.差集等场景很常用,下面来说下两个数组取差集 直接上代码: //两个集合取并集 package main import "fmt" //思想: / ...

  6. 定时ping取返回值并绘图

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  7. php取默认值以及类的继承

    (1)对于php的默认值的使用和C++有点类似,都是在函数的输入中填写默认值,以下是php方法中对于默认值的应用: <?phpfunction makecoffee($types = array ...

  8. java中两个Integer类型的值相比较的问题

    今天在做一个算法时,由于为了和其他人保持接口的数据类型一致,就把之前的int换为Integer,前几天测了几组数据,和之前的结果一样,但是今天在测其它数据 的时候,突然出现了一个奇怪的bug,由于之前 ...

  9. 在android的spinner中,实现取VALUE值和TEXT值。 ZT

    在android的spinner中,实现取VALUE值和TEXT值.   为了实现在android的 spinner实现取VALUE值和TEXT值,我尝试过好些办法,在网上查的资料,都是说修改适配器, ...

随机推荐

  1. Git 分支

    Git 保存的不是文件的变化或者差异,而是一系列不同时刻的文件快照,某一次的提交指向这处时刻的文件快照,看起来就像每次提交都保存了当时的文件,连续的提交形成一条长链 分支 指向某一个特定的提交,不同的 ...

  2. C++中的static关键字的总结

    C++的static有两种用法:面向过程程序设计中的static和面向对象程序设计中的static.前者应用于普通变量和函数,不涉及类:后者主要说明static在类中的作用. 1.面向过程设计中的st ...

  3. html5移动web开发笔记(一)Web 存储

    localStorage - 没有时间限制的数据存储 localStorage 方法 localStorage 方法存储的数据没有时间限制.第二天.第二周或下一年之后,数据依然可用. 用户访问页面的次 ...

  4. LLDB, iOS调试器

    breakpoint set -s "" breakpoint set    -M <method> ( --method <method> )    -S ...

  5. SSH整合开发的web.xml配置

    <?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" ...

  6. 重读高程3: c2-3 script元素

    1. 异步脚本一定会在页面的load事件前执行,但可能会在domC ontentLoaded事件触发之前或者之后执行.

  7. Hibernate的增删改查

    一.搭建Hibernate开发环境,这里就不说了,直接说环境搭好后的事情. 二.项目的目录结构

  8. css cursor 的可选值(鼠标的各种样式)

    crosshair; 十字准心 The cursor render as a crosshair游标表现为十字准线 cursor: pointer; cursor: hand;写两个是为了照顾IE5, ...

  9. 移动混合开发之android文件管理-->flexbox,webFont。

    增加操作栏,使用felxbox居中,felx相关参考网址:http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html 使用webFont添加图标, ...

  10. mount: /dev/sdb1 already mounted or /mnt/hdb busy 导致NameNode无法启动

    最近,公司由于断电导致的服务器关机,等到来电了,重启集群发现Namenode无法启动,查看原因是由于无法加在faimage文件,在查看一下Namenode的磁盘挂在情况(df -h命令查看),发现磁盘 ...