quma
quma

Reputation: 5733

Java-8 simplify lambda expression with embedded Streams

Is there a more simple and performant way of doing this, At the end I would need a list of scheduleContainers (List<ScheduleContainer>)

final List<ScheduleResponseContent> scheduleResponseContents = new ArrayList<>();
scheduleResponseWrappers.parallelStream().forEach(srw -> scheduleResponseContents.addAll(srw.getScheduleResponseContents()));
final List<List<ScheduleContainer>> schedulesOfWeek = new ArrayList<>();
scheduleResponseContents.parallelStream().forEach(src -> schedulesOfWeek.addAll(src.getSchedules()));
final List<ScheduleContainer> schedules = new ArrayList<>();
schedulesOfWeek.parallelStream().forEach(s -> schedules.addAll(s));

Upvotes: 0

Views: 990

Answers (1)

bartac
bartac

Reputation: 106

Because of missing classes, I can just assume this is correct:

final List<ScheduleContainer> schedules = scheduleResponseWrappers.stream()
    .flatMap(srw -> srw.getScheduleResponseContents().stream())
    .flatMap(src -> src.getSchedules().stream())
    .collect(Collectors.toList());

Upvotes: 1

Related Questions