2016-06-21 22 views
0

未包含的元素的返回列表我想保留兩個ArrayLists中的所有不同元素,並將它們保存在單獨的數組中。我有本地和遠程ArrayList(本地在我的設備上,我通過從本地數據庫讀取來創建它)。遠程,你可以猜到我從我的服務器。 我想要獲得本地和遠程之間的所有差異,以便通過id刪除本地列表中的所有元素(假設遠程lsit將包含最新數據)。另一個[JAVA/ANDROID]

我的遙控器列表是這個:

[ 
{ 
"userID": 8, 
"address": "6 Lamond Place, Aberdeen, AB25 3UT", 
"price": 1, 
"lastUpdated": "1466437965391", 
"id": 175 
}, 
{ 
"userID": 8, 
"address": "26 Meadgrey, Edinburgh, EH4", 
"price": 4, 
"lastUpdated": "1466438561094", 
"id": 176 
} 
] 

我的本地列表包含此數據:

[ 
Property{id=174, userID=8, address='4', price='3', lastUpdated='1466437959249'}, 
Property{id=175, userID=8, address='6 Lamond Place, Aberdeen, AB25 3UT', price='1', lastUpdated='1466437965391'}, 
Property{id=176, userID=8, address='26 Meadgrey, Edinburgh, EH4', price='4', lastUpdated='1466438561094'} 
] 

這就是我現在所做的:

public void retainAllLocalFromRemote(List<Property> remoteProperties) { 
    ArrayList<Property> localProperties = getAllPropertyItems(); 

    Log.d(TAG, "Local db size: " + localProperties.size()); 
    Log.d(TAG, "Remote db size: " + remoteProperties.size()); 

    ArrayList<Property> differences = new ArrayList<>(); 

    for(int i = 0; i < localProperties.size(); i ++){ 

      if(remoteProperties.contains(localProperties.get(i))){ 
       Log.d(TAG, "remote contains local property with id: " + localProperties.get(i).getId()); 
      }else { 
       differences.add(localProperties.get(i)); 
      } 

    } 

    Log.d(TAG, "differences list size: " + differences.size()); 
    Log.d(TAG, "differences list : " + differences.toString()); 
} 

這是我目前的日誌輸出:

Getting all properties from the database... 
06-21 09:54:50.965 10585-10585/xdesign.georgi.espc_retrofit D/EspcItemDataSource: Local db size: 3 
06-21 09:54:50.965 10585-10585/xdesign.georgi.espc_retrofit D/EspcItemDataSource: Remote db size: 2 
06-21 09:54:50.966 10585-10585/xdesign.georgi.espc_retrofit D/EspcItemDataSource: differences list size: 3 
06-21 10:00:48.192 10585-10585/xdesign.georgi.espc_retrofit D/EspcItemDataSource: differences list :[ 
Property{id=174, userID=8, address='4', price='3', lastUpdated='1466437959249'}, 
Property{id=175, userID=8, address='6 Lamond Place, Aberdeen, AB25 3UT', price='1', lastUpdated='1466437965391'}, 
Property{id=176, userID=8, address='26 Meadgrey, Edinburgh, EH4', price='4', lastUpdated='1466438561094'}] 

顯然,由於差異列表的大小應該是錯誤的。我做錯了什麼?

+2

Property.equals()是什麼樣的?如果你已經實現了它,那麼 –

+0

@TimCastelijns不,我沒有在我的Property類中實現它。這就是爲什麼我得到錯誤的等於結果? –

回答

3

我在做什麼錯了?

List.contains()使用Object.equals()比較項目,看看他們是在一個列表或沒有。如果你沒有實現它,我相信它使用toString值或者作爲後備來比較項目。這會弄亂你的比較。

由於這個原因,當前環路中的所有項目都會因爲而被假定,並且它們全部被添加到differences。您需要在class Property中提供equals()hashCode()的自定義實現,以便您可以決定某個對象何時與另一個對象相同,何時不相等。

它似乎在這裏直接的做

@Override 
public boolean equals(Object o) { 
    if (this == o) return true; 
    if (!(o instanceof Property)) return false; 
    Property prop = (Property) o; 
    return Objects.equals(id, prop.id); 
} 

@Override 
public int hashCode() { 
    return Objects.hash(id); 
} 

要通過ID進行比較。如果您想通過其他值進行比較,則可以根據需要進行更改。

+0

工作!謝謝!完全忘了重寫equals方法!謝謝!我花了很多時間試圖弄清楚我做錯了什麼!欣賞它! –

+1

@GeorgiKoemdzhiev沒問題。我知道這一點的唯一原因是因爲我自己曾犯過同樣的錯誤 –

+1

我會改變'o == null || getClass()!= o.getClass()'帶'(!(o instanceof Property))' – Blackbelt