I was wondering how I could use Java Stream API to flatten a structure where I have an object and a collection of the same type objects nested in it.
What I mean is that I have a Component class which has a field of type List<Component>. What I would like to do is find a neat, stream solution that would the the same as the following code (I need to end up with a list of all components and nested subcomponents).
List<Component> components = getComponents(id); List<Component> componentsAndSubcomponents = new ArrayList<>(); for (Component component : components) { componentsAndSubcomponents.add(component); componentsAndSubcomponents.addAll(component.getSubComponents()); }