測試說明:獲取firstArrayList和secondArrayList的交集、差集以及並集。實際測試中firstArrayList數據量190000,secondArrayList數據量170000.效率比較高。此處只列出少量數據。測試代碼如下:
import java.util.Set;
import java.util.List; import java.util.HashSet; import java.util.TreeSet; import java.util.Iterator; import java.util.ArrayList; import java.util.LinkedList; public class getSet { public static void main(String args[]) { getList(); } // 獲取兩個ArrayList的差集、交集、去重並集(數據量大小不限制) private static void getList() { List<String> firstArrayList = new ArrayList<String>(); List<String> secondArrayList = new ArrayList<String>(); List<String> defectList = new ArrayList<String>();//差集List List<String> collectionList = new ArrayList<String>();//交集List List<String> unionList = new ArrayList<String>();//去重並集List try { firstArrayList.add("aaa"); firstArrayList.add("bbb"); firstArrayList.add("ccc"); firstArrayList.add("ddd"); secondArrayList.add("bbb"); secondArrayList.add("ccc"); secondArrayList.add("eee"); // 獲取差集 defectList = receiveDefectList(firstArrayList, secondArrayList); Iterator<String> defectIterator = defectList.iterator(); System.out.println("===================差集==================="); while(defectIterator.hasNext()) { System.out.println(defectIterator.next()); } // 獲取交集 collectionList = receiveCollectionList(firstArrayList, secondArrayList); Iterator<String> collectionIterator = collectionList.iterator(); System.out.println("===================交集==================="); while(collectionIterator.hasNext()) { System.out.println(collectionIterator.next()); } // 獲取去重並集 unionList = receiveUnionList(firstArrayList, secondArrayList); Iterator<String> unionIterator = unionList.iterator(); System.out.println("===================去重並集==================="); while(unionIterator.hasNext()) { System.out.println(unionIterator.next()); } }catch(Exception e) { e.printStackTrace(); } } /** * @方法描述:獲取兩個ArrayList的差集 * @param firstArrayList 第一個ArrayList * @param secondArrayList 第二個ArrayList * @return resultList 差集ArrayList */ public static List<String> receiveDefectList(List<String> firstArrayList, List<String> secondArrayList) { List<String> resultList = new ArrayList<String>(); LinkedList<String> result = new LinkedList<String>(firstArrayList);// 大集合用linkedlist HashSet<String> othHash = new HashSet<String>(secondArrayList);// 小集合用hashset Iterator<String> iter = result.iterator();// 采用Iterator迭代器進行數據的操作 while(iter.hasNext()){ if(othHash.contains(iter.next())){ iter.remove(); } } resultList = new ArrayList<String>(result); return resultList; } /** * @方法描述:獲取兩個ArrayList的交集 * @param firstArrayList 第一個ArrayList * @param secondArrayList 第二個ArrayList * @return resultList 交集ArrayList */ public static List<String> receiveCollectionList(List<String> firstArrayList, List<String> secondArrayList) { List<String> resultList = new ArrayList<String>(); LinkedList<String> result = new LinkedList<String>(firstArrayList);// 大集合用linkedlist HashSet<String> othHash = new HashSet<String>(secondArrayList);// 小集合用hashset Iterator<String> iter = result.iterator();// 采用Iterator迭代器進行數據的操作 while(iter.hasNext()) { if(!othHash.contains(iter.next())) { iter.remove(); } } resultList = new ArrayList<String>(result); return resultList; } /** * @方法描述:獲取兩個ArrayList的去重並集 * @param firstArrayList 第一個ArrayList * @param secondArrayList 第二個ArrayList * @return resultList 去重並集ArrayList */ public static List<String> receiveUnionList(List<String> firstArrayList, List<String> secondArrayList) { List<String> resultList = new ArrayList<String>(); Set<String> firstSet = new TreeSet<String>(firstArrayList); for(String id : secondArrayList) { // 當添加不成功的時候 說明firstSet中已經存在該對象 firstSet.add(id); } resultList = new ArrayList<String>(dawjidSet); return resultList; } }
打印結果:
===================差集===================
aaa
ddd
===================交集=================== bbb ccc =================去重並集================== aaa bbb ccc ddd eee
說明,取差集指的是取firstArrayList中存在但secondArrayList中不存在的數據集