zoukankan      html  css  js  c++  java
  • 查找两个List集合中的不同元素

    1. package com.czp.test;  
    2.   
    3. import java.util.ArrayList;  
    4. import java.util.HashMap;  
    5. import java.util.List;  
    6. import java.util.Map;  
    7.   
    8. public class TestList {  
    9.   
    10.     public static void main(String[] args) {  
    11.         List<String> list1 = new ArrayList<String>();  
    12.         List<String> list2 = new ArrayList<String>();  
    13.         for (int i = 0; i < 10000; i++) {  
    14.             list1.add("test"+i);  
    15.             list2.add("test"+i*2);  
    16.         }  
    17.       
    18.         getDiffrent5(list1,list2);  
    19.       
    20.   
    21. //        getDiffrent5 total times 12239545  
    22.                  
    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.     } 
  • 相关阅读:
    Open Live Writer增加代码插件
    WinSCP列出’/’目录项出错
    Ueditor中增加迅雷下载支持
    Ueditor设置默认字体
    PDF编辑、删除、替换某页面或文字
    个人站长如何使用svn发布到服务器不遗漏文件
    PHP 测试程序运行时间 microtime函数用法
    LeetCode---Stack && Heap
    LeetCode---Binary Search
    LeetCode---Hash Table
  • 原文地址:https://www.cnblogs.com/JeffXia/p/7015883.html
Copyright © 2011-2022 走看看