I just want to explain one more scenario like List<Documents>
, this list contains a few more lists of other documents like List<Excel>
, List<Word>
, List<PowerPoint>
. So the structure is
class A {
List<Documents> documentList;
}
class Documents {
List<Excel> excels;
List<Word> words;
List<PowerPoint> ppt;
}
Now if you want to iterate Excel only from documents then do something like below..
So the code would be
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.
}
I hope this can solve someone's issue while coding...