java 較大數(shù)據(jù)量取差集,list.removeAll性能優(yōu)化詳解
今天在優(yōu)化項目中的考勤同步功能時遇到將考勤機(jī)中的數(shù)據(jù)同步到數(shù)據(jù)庫,
兩邊都是幾萬條數(shù)據(jù)的樣子,老代碼的做法差不多半個小時,優(yōu)化后我本機(jī)差不多40秒,服務(wù)器速度會更加理想。
兩個數(shù)據(jù)集取差集首先想到的方法便是List.removeAll方法,但是實驗發(fā)現(xiàn)jdk自帶的List.removeAll效率很低
List.removeAll效率低原因:
List.removeAll效率低和list集合本身的特點有關(guān) :
List底層數(shù)據(jù)結(jié)構(gòu)是數(shù)組,查詢快,增刪慢
1.List.contains()效率沒有hashset高
arrayList.removeAll底層是for循化調(diào)用contains方法。arrayList雖然用get(index)方法查詢效率高,但是若用contains方法查詢對象元素,Set集合應(yīng)該比List效率要高。
因為hashset的contains方法其實是先調(diào)用每個元素的hashCode()方法來返回哈希碼,如果哈希碼的值相等的情況下再調(diào)用equals(obj)方法去判斷是否相等,只有在這兩個方法所返回的值都相等的情況下,才判定這個HashSet包含某個元素,而list直接調(diào)用equals(obj)方法.所以hashset效率更高。
2.arrayList.remove()效率沒有l(wèi)inkedList刪除效率高
arrayList底層采用數(shù)組每刪除一下元素數(shù)據(jù)后面的元素都要往前移動效率低消耗的資源也大,linkedList鏈表刪除元素只要改變前后節(jié)點的位置信息
3.采用Iterator迭代器,這種方式我們僅需要對iterator進(jìn)行循環(huán),然后對需要刪除的元素執(zhí)行iterator.remove(iterator.next()),而無需關(guān)注下標(biāo)的問題
改進(jìn)代碼
LinkedList linkedList= new LinkedList(src);//大集合用linkedlist HashSet hashSet= new HashSet(oth);//小集合用hashset Iterator iter = linkedList.iterator();//采用Iterator迭代器進(jìn)行數(shù)據(jù)的操作 while(iter.hasNext()){ if(hashSet.contains(iter.next())){ iter.remove(); } }
補(bǔ)充知識:JAVA獲取兩個數(shù)據(jù)量較大的ArrayList的交集、差集以及并集
測試說明:獲取firstArrayList和secondArrayList的交集、差集以及并集。實際測試中firstArrayList數(shù)據(jù)量190000,secondArrayList數(shù)據(jù)量170000.效率比較高。此處只列出少量數(shù)據(jù)。測試代碼如下:
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的差集、交集、去重并集(數(shù)據(jù)量大小不限制) 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迭代器進(jìn)行數(shù)據(jù)的操作 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迭代器進(jìn)行數(shù)據(jù)的操作 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) { // 當(dāng)添加不成功的時候 說明firstSet中已經(jīng)存在該對象 firstSet.add(id); } resultList = new ArrayList<String>(dawjidSet); return resultList; } }
打印結(jié)果:
===================差集=================== aaa ddd ===================交集=================== bbb ccc =================去重并集================== aaa bbb ccc ddd eee
說明,取差集指的是取firstArrayList中存在但secondArrayList中不存在的數(shù)據(jù)集
以上這篇java 較大數(shù)據(jù)量取差集,list.removeAll性能優(yōu)化詳解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于log4j日志擴(kuò)展---自定義PatternLayout
這篇文章主要介紹了關(guān)于log4j日志擴(kuò)展---自定義PatternLayout,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12Springboot通過請求頭獲取當(dāng)前用戶信息方法詳細(xì)示范
這篇文章主要介紹了Springboot通過請求頭獲取當(dāng)前用戶信息的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-11-11淺談SpringBoot @Autowired的兩種注入方式
本文主要介紹了兩種SpringBoot @Autowired注入方式,具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-06-06Eclipse+Java+Swing實現(xiàn)斗地主游戲(代碼)
這篇文章主要介紹了Eclipse+Java+Swing實現(xiàn)斗地主游戲并附上詳細(xì)的代碼實現(xiàn),正在學(xué)習(xí)的你可以當(dāng)小練習(xí)練練,希望對你有所幫助2022-01-01