未包含的元素的返回列表我想保留兩個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'}]
顯然,由於差異列表的大小應該是錯誤的。我做錯了什麼?
Property.equals()是什麼樣的?如果你已經實現了它,那麼 –
@TimCastelijns不,我沒有在我的Property類中實現它。這就是爲什麼我得到錯誤的等於結果? –