25

While learning Java 8 streams and lambas, I tried to replace the following nested for loops with streams :

List<Long> deskIds = new ArrayList<>();
for(ProvidedService memberService : service.getAllNodesDepthFirst()){
   for(Desk d : memberService.getDesks()){
     deskIds.add(d.getId());
   }
}

The loop iterates a list of 'ProvidedService' objects, and for each one, iterates over a list property of 'Desk' objects, and extracts the 'Id' field to a list.

I came up with the following code using streams :

List<Long> deskIds = new ArrayList<>();
service.getAllNodesDepthFirst().stream().forEach(srv -> {
    deskIds.addAll(srv.getDesks().stream().map(Desk::getId).collect(Collectors.toList()));
});

Is it the proper/optimal way to do it ? Or is there a way to do this without the second nested stream ?

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
Pierre Henry
  • 16,658
  • 22
  • 85
  • 105

1 Answers1

33

I would probably write it like this:

List<Long> deskIds = service.getAllNodesDepthFirst().stream()
                                          .flatMap(p -> p.getDesks().stream())
                                          .map(Desk::getId)
                                          .collect(toList());
assylias
  • 321,522
  • 82
  • 660
  • 783
  • 1
    Yes, I was hoping there was something like 'flatMap' ! – Pierre Henry Nov 27 '14 at 16:51
  • @assylias Is there any easy way if I want to get a map in the end instead of a List, where map being some id of ProvidedService and value being desk id. – Naman Sep 29 '16 at 18:58
  • @assylias I looked at groupingBy but it seems like I can get Map> but I don't want that, I was hoping to get Map . Can you suggest if ho I can get that? – Naman Sep 30 '16 at 18:50
  • It would make sense to ask a separate question for your specific see case. – assylias Oct 01 '16 at 00:48