title: Java 8 Stream + Map + ForEach + Collect
body: <p>I am reading values from a File and get List.
  like below - </p>
  
  <pre><code>List&lt;String&gt; fields = Utils.readFile("myText.txt", true);
  </code></pre>
  
  <p>Once I get this <code>List&lt;String&gt;</code> I want to iterate through this list and pass each value to a function which uses this value, queries into DB and prepares a <code>List&lt;MyCustomObject&gt;</code> and returns the same.</p>
  
  <p>Implementing in Java 7 is quite easy but I'm unable to get it in Java 8 using Stream + Map/forEach + Collect operations.</p>
  
  <p>I have tried the below - </p>
  
  <pre><code>Utils.readFile("myText-"+".txt", true)
          .stream()
          .map(str -&gt; getListOfCustomObjForEachStr(str, pstmt))
          .collect(Collectors.toList());
  </code></pre>
  
  <p>But it returns a <code>List&lt;List&lt;MyCustomObject&gt;&gt;</code> instead of merged <code>List&lt;MyCustomObject&gt;</code></p>
  
  <p>getListOfCustomObjForEachStr() method returns a List for each input str passed in the above code. And I need a merged List</p>
  
  <p>Can someone please guide me what can be done here to get a merged list? (I'm new to Java 8 so it might seem a silly question but any help is appreciated)</p>
  
  <p><strong>Edit 1</strong>
  Got this implemented like below using FlatMap -
  Kindly suggest if any other better way to do it. Hope it helps someone somewhere someday :) Keep sharing.</p>
  
  <pre><code>List&lt;MyCustomObjects&gt; = Utils.readFile("myText-"+".txt", true)
      .stream()
      .map(str -&gt; getListOfCustomObjForEachStr(str, pstmt))
      .flatMap(Collection::stream)
      .collect(Collectors.toList());
  </code></pre>