다음 두 가지 개체가 있다고 가정합니다.
public class MasterObject {
Long masterId;
Long detailId;
String attribute1;
//...
}
public class DetailObject {
Long detailId;
String attribute2;
//..
}
10 개의 마스터 개체의 상위 집합 목록과 2 개의 세부 개체의 하위 집합 목록이 있습니다. 결국, attribute1 == attribute2를 확인한 후 적절한 세부 ID와 함께 10 개의 세부 개체 (마스터에서 정보 복사)의 새로운 집합이 필요합니다. 각 세부 개체에 대해 새 마스터 개체를 만들어야합니다. 이것은 내가 지금까지 생각 해낸 것이지만 틀 렸습니다. 내 댓글 블록을 참고하십시오.
List<DetailObject> updatedDetails = masterObjects.stream()
.filter(master -> {
return detail.stream()
.anyMatch(it -> master.getAttribute1() == detail.getAttribute2());
})
.map(master -> {
DetailObject detailObject = new DetailObject();
// Here is where I thought I could set the master Id in the detail object,
// but I no longer have access to the original detail stream
return detailObject;
})
.collect(Collectors.toList());
필자는 필터 후에 뭔가를해야한다고 확신하지만 필터는 물론 객체 자체가 필요할 때 부울을 반환합니다. 무엇을 ??
나와 동료가 내 문제에 대한 해결책을 찾았습니다. 다음과 같이 보입니다.
List<DetailObject> newDetails = details.stream()
.map(detail -> {
return masters.stream()
.filter(master -> filterLoans(detail, master))
.map(master -> createDetail(detail, master))
.collect(Collectors.toList());
})
.flatMap(List::stream)
.collect(Collectors.toList());
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다