Map<String,List<MeetupDTO>> employeesMeetups = new HashMap<>(employeesId.size());
for(String employeeId : employeesId) {
List<MeetupDTO> meetups = meetupRepository.
getAllByEmployeeId(employeeId).
stream().
map(meetupMapper::toDTO).
collect(Collectors.toList());
employeesMeetups.put(employeeId,meetups);
return employeesMeetups;
I want to change what's on top to the bottom.
return Collectors.toMap(//How do I add keys here?,
employeesId.
stream().
forEach(e->meetupRepository.getAllByEmployeeId(e).
stream().
map(meetupMapper::toDTO).
collect(Collectors.toList())
));
I don't know how to make the keys match their list, how can this be implemented?
You can use
Collectors.toMap(), this way:Important: while using the
toMap()collector, you need to ensure that the keys are unique, or else you might encounter anIllegalStateException.Edit
Using unique IDs,
stream()is enough, but in cases where values can repeat, you can usedistinct(), as 01000001 said.