#302 Reusing the Same DTO in Stream API
Find the logical error in the code. The code compiles and runs, but works incorrectly:
public class InstrumentListingMapperDto {
private long orgId;
private String instrumentId;
private String remarkId;
public InstrumentListingMapperDto() {
}
public InstrumentListingMapperDto setOrgId(long orgId) {
this.orgId = orgId;
return this;
}
public InstrumentListingMapperDto setInstrumentId(String orgId) {
this.instrumentId = orgId;
return this;
}
public InstrumentListingMapperDto setRemarkId(String remarkId) {
this.remarkId = remarkId;
return this;
}
}
// ...
public List<InstrumentListingMapperDto> mapOrgIdToInstrumentAndRemarkIds() {
List<Long> orgIds = List.of(1L, 2L, 3L);
Map<Long, String> instrumentsMap = Map.of(1L, "instId1", 2L, "instId2", 3L, "instId3");
Map<Long, String> remarkIdMap = Map.of(1L, "remId1", 2L, "remId2", 3L, "remId3");
var instrumentListingMapperDto = new InstrumentListingMapperDto();
return orgIds.stream()
.map(id -> instrumentListingMapperDto
.setOrgId(id)
.setInstrumentId(instrumentsMap.get(id))
.setRemarkId(remarkIdMap.get(id)))
.toList();
}
Question review
Sign in to start the dialogue
The mentor keeps the context of the current question and your previous messages.
Sign in
Available with Premium
This is just one of the solutions available with Premium. And more:
- 250+ advanced lectures
- 1300+ practice exercises
- 500+ solutions to problems from real interviews
- Personalized Java Hub certificates
- Expanded access to the AI mentor