給定這些物件
測驗
int one;
int two;
List<AnotherObject> testList.
另一個物件
string name;
int three;
List<AthirdObject> anotherList;
第三個物件
string surname;
int id;
假設我們有 List < Test > 如何在不使用嵌套 for 回圈的情況下到達 AThirdObject ,提前謝謝
uj5u.com熱心網友回復:
Stream::flatMap 假設在示例類中正確實作了 getter,這里應該使用它來提供對嵌套串列的最低級別的“訪問”:
List<Test> input = ...; // define input data
List<AThirdObject> nestedList = input
.stream() // Stream<Test>
.flatMap(t -> t.getTestList().stream()) // Stream<AnotherObject>
.flatMap(ao -> ao.getAnotherList().stream()) // Stream<AThirdObject>
.collect(Collectors.toList());
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/348394.html
