2016-06-21 1 views
0

두 개의 ArrayList에서 다른 요소를 모두 보유하고 별도의 배열에 저장하려고합니다. 로컬 및 원격 ArrayList가 있습니다 (로컬은 내 장치에 있으며 로컬 데이터베이스에서 읽음으로써 생성합니다). 리모컨은 내 서버에서 가져온 것 같습니다. 로컬 목록에서 모든 요소를 ​​id로 삭제하기 위해 로컬과 리모트 사이의 모든 차이점을 가져 오지 못하게하고 싶습니다 (리모트 위치에는 가장 최근의 데이터가 들어 있다고 가정). 다른 요소에 포함되지 않은 요소 목록 반환 [Java/ANDROID]

내 원격 목록

는이 하나

[ 
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()); 
} 

이가 내 로컬 목록은

[ 
{ 
"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 
} 
] 

이 데이터를 포함

내 현재 로그 출력 :

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에 추가됩니다. equals()hashCode()의 사용자 지정 구현을 class Property에 제공해야 개체가 다른 개체와 같은지 아닌지를 결정할 수 있습니다.

는 그것은 ID로 비교하기 위해

@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); 
} 

을 여기에 간단 보인다. 다른 값으로 비교하려는 경우 적절하게 변경할 수 있습니다.

+0

일했습니다! 고맙습니다! equals 메소드를 오버라이드하는 것을 완전히 잊었다! 고맙습니다! 내가 뭘 잘못했는지 알아 내려고 많은 시간을 보냈다! 고맙습니다! –

+1

@GeorgiKoemdzhiev 문제 없습니다. 내가 이것을 알고있는 유일한 이유는 내가 한번도 같은 실수를했기 때문이다. –

+1

'o == null || getClass()! = o.getClass()'(! (o instanceof Property)) ' – Blackbelt

관련 문제