How can I turn a List of Lists into a List in Java 8?

后端 未结 9 594
春和景丽
春和景丽 2020-11-22 04:49

If I have a List>, how can I turn that into a List that contains all the objects in the same iteration order
9条回答
  •  渐次进展
    2020-11-22 05:06

    We can use flatmap for this, please refer below code :

     List i1= Arrays.asList(1, 2, 3, 4);
     List i2= Arrays.asList(5, 6, 7, 8);
    
     List> ii= Arrays.asList(i1, i2);
     System.out.println("List>"+ii);
     List flat=ii.stream().flatMap(l-> l.stream()).collect(Collectors.toList());
     System.out.println("Flattened to List"+flat);
    

提交回复
热议问题