国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

java 較大數(shù)據(jù)量取差集,list.removeAll性能優(yōu)化詳解

瀏覽:16日期:2022-08-23 18:40:13

今天在優(yōu)化項目中的考勤同步功能時遇到將考勤機中的數(shù)據(jù)同步到數(shù)據(jù)庫,

兩邊都是幾萬條數(shù)據(jù)的樣子,老代碼的做法差不多半個小時,優(yōu)化后我本機差不多40秒,服務器速度會更加理想。

兩個數(shù)據(jù)集取差集首先想到的方法便是List.removeAll方法,但是實驗發(fā)現(xiàn)jdk自帶的List.removeAll效率很低

List.removeAll效率低原因:

List.removeAll效率低和list集合本身的特點有關 :

List底層數(shù)據(jù)結構是數(shù)組,查詢快,增刪慢

1.List.contains()效率沒有hashset高

arrayList.removeAll底層是for循化調(diào)用contains方法。arrayList雖然用get(index)方法查詢效率高,但是若用contains方法查詢對象元素,Set集合應該比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進行循環(huán),然后對需要刪除的元素執(zhí)行iterator.remove(iterator.next()),而無需關注下標的問題

改進代碼

LinkedList linkedList= new LinkedList(src);//大集合用linkedlistHashSet hashSet= new HashSet(oth);//小集合用hashsetIterator iter = linkedList.iterator();//采用Iterator迭代器進行數(shù)據(jù)的操作while(iter.hasNext()){if(hashSet.contains(iter.next())){iter.remove();}}

補充知識: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迭代器進行數(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迭代器進行數(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) { // 當添加不成功的時候 說明firstSet中已經(jīng)存在該對象 firstSet.add(id); } resultList = new ArrayList<String>(dawjidSet); return resultList; }}

打印結果:

===================差集===================aaaddd===================交集===================bbbccc=================去重并集==================aaabbbcccdddeee

說明,取差集指的是取firstArrayList中存在但secondArrayList中不存在的數(shù)據(jù)集

以上這篇java 較大數(shù)據(jù)量取差集,list.removeAll性能優(yōu)化詳解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。

標簽: Java
主站蜘蛛池模板: 辽阳县| 崇仁县| 合阳县| 兴海县| 金山区| 阳西县| 海林市| 孟津县| 建平县| 托里县| 和田县| 阳西县| 新晃| 桂阳县| 容城县| 云阳县| 子长县| 宕昌县| 济阳县| 南城县| 云龙县| 延长县| 青铜峡市| 铜川市| 湟源县| 随州市| 文登市| 苏州市| 龙江县| 荔波县| 彭泽县| 阿克陶县| 南充市| 金门县| 灯塔市| 和龙市| 云和县| 黄山市| 昌都县| 贵定县| 铜川市|