Java 8 FlatMap - Add items in a nested form and retrieve object using FlatMap
<p>I have an object <code>Foo</code> which contains a list of <code>Bar</code>. The classes are described as follows:</p>

<pre><code>class Foo {
    String name;
    List&lt;Bar&gt; bars = new ArrayList&lt;Bar&gt;();

    Foo(String name){
        this.name = name;
    }
}

class Bar {
    String name;

    Bar(String name){
        this.name = name;
    }
}
</code></pre>

<p>Now, i am creating a list of <code>Foo</code> objects, each containing a list of <code>Bar</code> objects as follows:</p>

<pre><code>IntStream
        .range(1, 4)
        .forEach(i -&gt; foos.add(new Foo("Foo" + i)));

foos.forEach(f -&gt;
            IntStream.range(1,4)
                    .forEach(i -&gt; f.bars.add(new Bar("Bar"+i+" -&gt; "+f.name))));
</code></pre>

<p>And then using <code>flatMap</code> on a <code>Stream</code> as follows:</p>

<pre><code>foos.stream()
        .flatMap(f -&gt; f.bars.stream())
        .forEach(i -&gt; System.out.println("Bar Name : "+i.name));
</code></pre>

<p>How can do all these things in a single execution using Java <code>Stream</code> and lambdas? Is there any other way to do such kind of things with Java 8 style?</p>