高效篩選兩個List中的不同的元素
問題記錄:
開發過程中,需要把兩個List中不同的元素篩選出來,這兩個List的資料量都很大,如果按照一般的方法,分別去遍歷兩個List,然後分別對每一個元素做比較,時間消耗將會達到m*n,處理效率顯然不盡人意。
解決思路:
使用一個Map來對2個List中的元素進行計數:
即把List的元素作為Map的Key,Entry的Value為Integer型別,用於記錄元素在兩個集合中出現的次數。
解決方案:
先遍歷一個List中的所有元素,put進Map,初始出現次數為1;
再遍歷第二個List中的所有元素,與map已有的元素進行比較:
如果Map中不存在這個元素,就把這個元素插入結果集,
如果Map中存在這個元素,則把這個元素的出現次數置為2。
程式碼示例:
示例實體類Product:
示例解決方法:public class Product { private Integer id; private String name; public Product(Integer id, String name) { this.id = id; this.name = name; } public Integer getId() { return id; } public String getName() { return name; } @Override public String toString() { return "Product [id=" + id + ", name=" + name + "]"; } public boolean equals(Object o){ if (o == null) { return false; } if (this == o) { return true; } if (o instanceof Product) { Product p = (Product) o; if (p.getId() == this.getId() && p.getName().equals(this.getName())) { return true; }else { return false; } } return false; } public int hashCode(){ int result = 17; result = result*37 + id; result = result*37 + name.hashCode(); return result; } }
測試程式碼:public static Collection<Product> getDiffrent(Collection<Product> col1, Collection<Product> col2){ //建立返回結果 Collection<Product> diffrentResult = new ArrayList<>(); //比較出兩個集合的大小,在新增進map的時候先遍歷較大集合,這樣子可以減少沒必要的判斷 Collection<Product> bigCol = null; Collection<Product> smallCol = null; if (col1.size() > col2.size()) { bigCol = col1; smallCol = col2; }else { bigCol = col2; smallCol = col1; } //建立 Map<物件,出現次數> (直接指定大小減少空間浪費) Map<Object, Integer> map = new HashMap<>(bigCol.size()); //遍歷大集合把元素put進map,初始出現次數為1 for(Product p : bigCol) { map.put(p, 1); } //遍歷小集合,如果map中不存在小集合中的元素,就新增到返回結果,如果存在,把出現次數置為2 for(Product p : smallCol) { if (map.get(p) == null) { diffrentResult.add(p); }else { map.put(p, 2); } } //把出現次數為1的 Key:Value 撈出,並把Key新增到返回結果 for(Map.Entry<Object, Integer> entry : map.entrySet()) { if (entry.getValue() == 1) { diffrentResult.add((Product) entry.getKey()); } } return diffrentResult; }
public static void main(String[] args) {
List<Product> list1 = new ArrayList<>();
List<Product> list2 = new ArrayList<>();
for (int i = 0; i < 10; i++) {
list1.add(new Product(i, "Product"+String.valueOf(i)));
}
for (int i = 0; i < 10; i = i + 2) {
list2.add(new Product(i, "Product"+String.valueOf(i)));
}
Collection<Product> result = getDiffrent(list1, list2);
for(Product p : result) {
System.out.println(p.toString());
}
}
測試結果:
Product [id=7, name=Product7]
Product [id=1, name=Product1]
Product [id=9, name=Product9]
Product [id=3, name=Product3]
Product [id=5, name=Product5]
解決過程中遇到的問題:
由於是把自定義類作為Map的Key,勢必會存在一個問題:
Map在get的時候,是沒有辦法直接get到這個Key對應的鍵值對的。
解決辦法:
由HashMap的get方法的原始碼:
public V get(Object key) {
if (key == null)
return getForNullKey();
Entry<K,V> entry = getEntry(key);
return null == entry ? null : entry.getValue();
}
再來看看getEntry方法的原始碼:
final Entry<K,V> getEntry(Object key) {
if (size == 0) {
return null;
}
int hash = (key == null) ? 0 : hash(key);
for (Entry<K,V> e = table[indexFor(hash, table.length)];
e != null;
e = e.next) {
Object k;
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
}
return null;
}
由原始碼可以看出,HashMap在根據Key查詢的時候,是根據hashCode的值和equals方法來查詢這個Key所對應的鍵值對的,顯然我們需要重寫自定義類的equals()方法和hashCode()方法。
由於這裡只需要判斷物件的邏輯相等,重寫的equals()方法只需要判斷各個屬性值是否相等即可
public boolean equals(Object o){
if (o == null) {
return false;
}
if (this == o) {
return true;
}
if (o instanceof Product) {
Product p = (Product) o;
if (p.getId() == this.getId() && p.getName().equals(this.getName())) {
return true;
}else {
return false;
}
}
return false;
}
重寫hashCode()方法
學習了《Effective Java》中提出的一種簡單通用的hashCode演算法
1. 初始化一個整形變數,為此變數賦予一個非零的常數值,比如int result = 17;
2. 選取equals方法中用於比較的所有域,然後針對每個域的屬性進行計算:
(1) 如果是boolean值,則計算f ? 1:0
(2) 如果是byte\char\short\int,則計算(int)f
(3) 如果是long值,則計算(int)(f ^ (f >>> 32))
(4) 如果是float值,則計算Float.floatToIntBits(f)
(5) 如果是double值,則計算Double.doubleToLongBits(f),然後返回的結果是long,再用規則(3)去處理
long得到int
(6) 如果是物件應用,如果equals方法中採取遞迴呼叫的比較方式,那麼hashCode中同樣採取遞迴呼叫
hashCode的方式。否則需要為這個域計算一個正規化,比如當這個域的值為null的時候,那麼hashCode值為0。
(7) 如果是陣列,那麼需要為每個元素當做單獨的域來處理。如果你使用的是1.5及以上版本的JDK,那麼沒
必要自己去重新遍歷一遍陣列,java.util.Arrays.hashCode方法包含了8種基本型別陣列和引用陣列的
hashCode計算,演算法同上。
public int hashCode(){
int result = 17;
if (id != null) {
result = result*37 + id;
}
if (name != null) {
result = result*37 + name.hashCode();
}
return result;
}
至此問題完美解決,總的來說這是一個以空間換時間的解決方案。