I have a java class
class Example{
String field1;
String field2;
List<Example> subExamples;
}
In the above scenario, Example is having subExamples which is again a list of Examples. This nesting can be n-levels. What I want to achieve is to have a list of Examples, i.e to flatten the above object and collect all the Examples into a final list (collecting all n-level examples). One obvious way is recursion. is there any way in Java I can achieve it more efficiently. I tried some java 8 concepts but they're not fitting into the requirement.