class A {List<Documents> documentList;}
class Documents {List<Excel> excels;List<Word> words;List<PowerPoint> ppt;}
现在,如果您只想从文档迭代Excel,请执行以下操作。
所以代码是
List<Documents> documentList = new A().getDocumentList();
//check documentList as not null
Optional<Excel> excelOptional = documentList.stream().map(doc -> doc.getExcel()).flatMap(List::stream).findFirst();if(excelOptional.isPresent()){Excel exl = optionalExcel.get();// now get the value what you want.}
public class Example {
public static void main(String[] args) {List<List<String>> listOfLists = Collections.singletonList(Arrays.asList("a", "b", "v"));List<String> list = listOfLists.stream().flatMap(List::stream).collect(Collectors.toList());
System.out.println("listOfLists => " + listOfLists);System.out.println("list => " + list);}
}
它打印:
listOfLists => [[a, b, c]]list => [a, b, c]
在Python中,这可以使用列表理解来完成。
list_of_lists = [['Roopa','Roopi','Tabu', 'Soudipta'],[180.0, 1231, 2112, 3112], [130], [158.2], [220.2]]
flatten = [val for sublist in list_of_lists for val in sublist]
print(flatten)