I have a AssistantList and need to map to Map of (Key - Assistant Login, Value - ArrayList of its Supervisor)
I used below code to map using for loop.
How can I use the Stream API to achieve the same functionality?
for(Assistant assistant: assistantList)
{
for(Supervisor supervisor: assistant.getSupervisorList())
{
map.computeIfAbsent(assistant.getLogin(),
k->new ArrayList<>()).add(supervisor.getLogin());
}
}
assistantList.stream() .collect(groupingBy(Assistant::getLogin, Collectors.mapping(Assistant::getSupervisorList, Collectors.collectingAndThen(Collectors.toList(), lists -> lists.stream().flatMap(List::stream).map(Supervisor::getLogin).collect(Collectors.toList())))));for(Assistant assistant: assistantList) { List<…> l = map.computeIfAbsent(assistant.getLogin(), k->new ArrayList<>()); for(Supervisor supervisor: assistant.getSupervisorList()) l.add(supervisor.getLogin()); }