- package com.czp.test;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- public class TestList {
- 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);
- }
- getDiffrent5(list1,list2);
- // getDiffrent5 total times 12239545
- }
- /**
- * 获取两个List的不同元素
- * @param list1
- * @param list2
- * @return
- */
- 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;
- }