1

I have a use-case in Java where I need to populate one of the lists (say x) based on the id of the other list(say y) and to fetch the result from that list.

List<LightRecruiterScholarResponse> responses = eventScholarRepository.findScholarDetailsByEventId(eventId);
List<InterviewDto> interviewResults = interviewRepository.getInterviewResultByRoundIdAndScholarId();

for (LightRecruiterScholarResponse response : responses) {
    String val = null;

    for (InterviewDto dto : interviewResults) {
        if (dto.getId().equals(response.getScholarId())) {
            val = dto.getInterviewResult();
            break;
        }
    }

    response.setInterviewStatus(val);
}
2

2 Answers 2

1

You can use:

Map<String, String> map = interviewResults.stream()
      .collect(Collectors.toMap(InterviewDto::getId, InterviewDto::getInterviewResult));

responses.forEach(response ->
        response.setInterviewStatus(
                map.getOrDefault(response.getScholarId(), null)));

The idea is to create a Map of Id for key and InterviewResult for value, and then for each element in responses you set InterviewStatus which you can find it in the map by ScholarId which can replace if (dto.getId().equals(response.getScholarId()))

Sign up to request clarification or add additional context in comments.

Comments

1

This could be done straightforward:

List<LightRecruiterScholarResponse> responses =
         eventScholarRepository.findScholarDetailsByEventId(eventId);
List<InterviewDto> interviewResults =
         interviewRepository.getInterviewResultByRoundIdAndScholarId();

responses.forEach(response -> response.setInterviewStatus(
                 interviewResults.stream()
                        .filter(dto -> dto.getId().equals(response.getScholarId()))
                        .map(InterviewDto::getInterviewResult)
                        .findFirst().orElse(null)));

This is not very efficient, because you iterate over interviewResults for every response. To fix it, you can build Map and use it:

List<LightRecruiterScholarResponse> responses =
         eventScholarRepository.findScholarDetailsByEventId(eventId);
Map<String, String> interviewResults =
         interviewRepository.getInterviewResultByRoundIdAndScholarId().stream()
                    .collect(Collectors.toMap(InterviewDto::getId,
                                              InterviewDto::getInterviewResult)); 
responses.forEach(response ->
   response.setInterviewStatus(interviewResults.get(response.getScholarId())));

2 Comments

@oleg.cherednik I tried the first approach but it seems to be giving NulPointerException. Basically, I need to set the interviewResult field for the list named responses and return that list from the method.
@oleg.cherednik in my use case there might be a situation where there can be multiple null values being returned by the method getInterviewResult.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.