Just recently involved to find different elements from two different sets of needs, the following test code
1, using the method of the apache collection tool, attached coordinates
<dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.1</version> </dependency>
collection kit gives a convenient tool 2 Method
1, to find common elements org.apache.commons.collections.ListUtils.retainAll (Collection, Collection) accompanied by source as follows
public static List removeAll(Collection collection, Collection remove) { List list = new ArrayList(); for (Iterator iter = collection.iterator(); iter.hasNext();) { Object obj = iter.next(); if (remove.contains(obj) == false) { list.add(obj); } } return list; }
2, to identify different elements org.apache.commons.collections.ListUtils.removeAll (Collection, Collection) as the source accompanied
public static List retainAll(Collection collection, Collection retain) { List list = new ArrayList(Math.min(collection.size(), retain.size())); for (Iterator iter = collection.iterator(); iter.hasNext();) { Object obj = iter.next(); if (retain.contains(obj)) { list.add(obj); } } return list; }
Attach test demo code
public static void main(String[] args) { // 生成集合1 List<Integer> list1 = Lists.newArrayList(); for (int i = 0; i < 100000; i++) { list1.add(i); } // 生成集合 2 List<Integer> list2 = Lists.newArrayList(); for (int i = 0; i < 100001; i++) { list2.add(i); } long start = System.currentTimeMillis(); // 开始分离 List<Integer> list = ListUtils.removeAll(list2, list1); long end = System.currentTimeMillis(); System.out.println(list); System.out.println(end - start); // 总执行次数 100000*100001 }
Execution results are as follows
[100000]
4027
As can be seen by a set of source code iterative process performed 100,000 * 100,001 times, as the amount of data, the speed will be slower and slower, so with the following optimization, the pay codes
public static void main(String[] args) { // 生成集合1 List<Integer> list1 = Lists.newArrayList(); for (int i = 0; i < 100000; i++) { list1.add(i); } // 生成集合2 List<Integer> list2 = Lists.newArrayList(); for (int i = 0; i < 100001; i++) { list2.add(i); } long start = System.currentTimeMillis(); //开始分离 Map<Integer, Integer> map = new HashMap<Integer, Integer>(); for (Integer integer : list2) { map.put(integer, 1); } for (Integer integer : list1) { map.put(integer, 2); } List<Integer> list3 = new ArrayList<Integer>(); Set<Entry<Integer, Integer>> entrySet = map.entrySet(); for (Entry<Integer, Integer> entry : entrySet) { Integer value = entry.getValue(); if (Objects.equals(1, value)) { list3.add (entry.getKey ()); } } // End isolated Long End = System.currentTimeMillis (); System.out.println (list3); System.out.println (End - Start); // total execution 100000 + 100001 + 100001 times }
Results of the
[100000]
33
The difference is very obvious, and we analyze the number of executions can be seen, 100000 + 100001 + 100001 a lot to reduce the number of iterations by performing a number of iterations map, speed up the natural
Summary: When the small amount of data, the still highly recommended by ListUtils.removeAll way, after all, do not have to create the wheel, but if the amount of data to more than one million, the recommended way to use the Map