集合去重,高效演算法

2021-09-02 18:50:17 字數 1289 閱讀 3215

我們最常用的兩個集合去重的方法是removeall,但是當兩個集合達到上萬之後就已經很慢了,上百萬之後,處理速度更是令人難以忍受,處理時間超過10分鐘以上,測試**如下:

public class test 

for(int i = 500000; i<=1500000; i++)

system.out.println("start: " + new date());

list2.removeall(list1);

system.out.println("end: " + new date());

}}

這個時候我們可以使用collectionutils的intersection方法(取交集)和disjunction方法(取差集),disjunction 就是我們要的去重之後的集合,**如下:

public class test 

for(int i = 500000; i<=1500000; i++)

system.out.println("start: " + new date());

collection intersection = collectionutils.intersection(list1, list2);

collection disjunction = collectionutils.disjunction(list2, intersection);

system.out.println("end: " + new date());

}}

測試結果:

start: thu dec 06 20:36:11 cst 2018

end: thu dec 06 20:36:12 cst 2018

單個集合資料量達到100萬,處理時間為1s,結果顯而易見。

原始碼如下:

public static collection intersection(collection a, collection b) 

}return list;

}public static collection disjunction(collection a, collection b)

}return list;

}public static map getcardinalitymap(collection coll) else

}return count;

}從原始碼不難發現,集合遍歷次數僅為集合a和集合b的並集的數量,通過map的使用極大的提高了處理速度。

利用set集合進行list集合高效去重

最近幫朋友做專案,自己也在學習資料結構和演算法分析,發現要想專案高效率的執行,資料結構和演算法是必須要掌握的一門技術,剛好專案中就碰到了處理資料的地方。首先我先說說需求吧,有兩個集合a b,在a集合去除b集合中people物件的資料,只要a集合中people的name和sublist集合中peopl...

高效網頁去重演算法 SimHash

記得以前有人問過我,網頁去重演算法有哪些,我不假思索的說出了余弦向量相似度匹配,但如果是數十億級別的網頁去重呢?這下糟糕了,因為每兩個網頁都需要計算一次向量內積,查重效率太低了!我當時就想 論查詢效率肯定是要考慮hash演算法,相同字串的hashcode肯定相同,不同字串的hashcode卻是大不相...

List集合去重

第一種 list每remove掉乙個元素以後,後面的元素都會向前移動,此時如果執行i i 1,則剛剛移過來的元素沒有被讀取。string str1 newstring abcde1 string str2 newstring abcde2 string str3 newstring abcde3 s...