2014-07-11 5 views
0

속성 :내가 ReportItem 개체의이 개 목록이

tcItems [ReportItem [date=2014-07-11 08:00:00.000,operator=Vodafone,country=Irl,blocked=1,total=1,uniqTriggeredCount=1,], ReportItem [date=2014-07-11 08:00:00.000,operator=TMob,country=UK,blocked=2,total=4,uniqTriggeredCount=3,]] 
fcItems [ReportItem [date=2014-07-11 08:00:00.000,filterid=1,country=Irl,operator=Vodafone,triggered=0,blocked=0,analysis=3,], ReportItem [date=2014-07-11 08:00:00.000,filterid=1,country=UK,operator=TMob,triggered=0,blocked=5,analysis=0,], ReportItem [date=2014-07-11 08:00:00.000,filterid=2,country=UK,operator=TMob,triggered=0,blocked=4,analysis=0,]] 

나는 두 목록 같은 것을 같은 날짜가 tcItems의 모든 reportItem, 국가에서 새 목록을 만들려면 fcItems에서 reportItem으로 연산자를 사용하면 두 속성을 결합한 새 목록에서 reportItem 객체를 가져옵니다. 위의 예에서 내가 원하는 : fcItems 여러 filterIds, triggereds, blockeds 및 analysises을 가질 수

newReportItems [ReportItem [date=2014-07-11 08:00:00.000,operator=Vodafone,country=Irl,blocked=1,total=1,uniqTriggeredCount=1,[filterid=1,triggered=0,blocked=0,analysis=3],], ReportItem [date=2014-07-11 08:00:00.000,operator=TMob,country=UK,blocked=2,total=4,uniqTriggeredCount=3,[filterid=1,triggered=0,blocked=5,analysis=0],[filterid=2,triggered=0,blocked=4,analysis=0],] 

참고.

본 적이 있지만이 두 개체 목록을 병합하는 방법을 찾을 수 없습니다. 같은 날, 국가 및 연산자를 가진 다른 ReportItem과 비교했을 때 true를 반환하는

1) 재정 ReportItemequals 방법 : 당신은 두 단계를 필요

+0

오오 질문을 형식을 지정하십시오. 읽을 수 없다. –

답변

1

도와주세요.

@Override 
public boolean equals(Object o) { 
    if (o instanceof ReportItem) { 
     ReportItem ri = (ReportItem) o; 
     if (this.getDate().equals(ri.getDate()) && ... // compare other attributes) { 
      return true; 
     } 
    } 
    return false; 
} 

1B)는 ReportItem을 수정할 수없는 경우 ReportItems 일치에 대한 compare 방법에 0을 반환하는 Comparator<ReportItem> 물품.

2) tcItems를 반복하고 각 항목 검사에 대해 fcItems에 포함되어 있는지 확인합니다 (equals 또는 comparator를 사용). 이 경우, 두 ReportItems을 가지고 그들을 결합 당신은 이제 당신은 또한 수집/필터 자바 (8)의 스트리밍 API를 사용할 수

for (ReportItem ri : tcItems) { 
    ReportItem matchingRI = findMatchingRI(ri); 
    if (matchingRI != null) { 
     // merge 
    } 
} 

private ReportItem findMatchingRI(ReportItem ri) { 
    for (ReportItem fcItem : fcItems) { 
     if (fcItem.equals(ri)) 
      return fcItem; 
    } 
    return null; 
} 

원하는대로,하지만 난 그것에 대해 세부 사항으로 가지 않을 것이다.