What is the proper way of replacing a nested for loop with streams in Java 8?

前端 未结 1 1825
迷失自我
迷失自我 2020-12-09 16:24

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

List deskIds = new ArrayList<>()         


        
相关标签:
1条回答
  • 2020-12-09 16:58

    I would probably write it like this:

    List<Long> deskIds = service.getAllNodesDepthFirst().stream()
                                              .flatMap(p -> p.getDesks().stream())
                                              .map(Desk::getId)
                                              .collect(toList());
    
    0 讨论(0)
提交回复
热议问题