如何避免此问题中的嵌套 for 循环

how to avoid a nested for loop in this problem

给定这些对象

测试

 int one;
 int two;
 List<AnotherObject> testList.

另一个对象

 string name;
 int three; 
 List<AthirdObject> anotherList;

ATThirdObject

 string surname;
 int    id;
 

假设我们有 List < Test > 我如何在不使用嵌套 for 循环的情况下到达 AThirdObject ,谢谢

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());