#302 Reusing the Same DTO in Stream API

Companies:
InfiniteSynergy
Themes:
JavaStreams & LambdasCode Review

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

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in