title: Usage of flatMap() in Java8
body: <p>I have an <code>Artist</code> class as follows:</p>
  
  <pre><code>class Artist {
      private final String name;
      private final String origin;
      private Stream&lt;Artist&gt; members;
  
      public Artist(String name, String origin) {
          this.name = name;
          this.origin = origin;
          this.members = null;
      }
  
      public Artist(String name, String origin,Stream&lt;Artist&gt; members) {
              this.name = name;
              this.origin = origin;
              this.members = members;
      }
  
      public String getName() {
              return name;
      }
  
      public String getOrigin() {
          return origin;
      }
  
      public Stream&lt;Artist&gt; getMembers() {
          return members;
      }
  
      @Override
      public String toString() {
          return name;
      }
  }
  </code></pre>
  
  <p>Now we create a list of artists, where the name of the artist is a band or a single entity. For single entity <code>"members"</code>attribute remains null.Now the list is as follows :</p>
  
  <pre><code>List&lt;Artist&gt; artists = Arrays.asList(
                  new Artist("Fossils","Kolkata", Stream.of(new Artist("Rupam Islam","Kolkata"), new Artist("Deep","Kolkata"),new Artist("Allan","Kolkata"), new Artist("Chandramouli","Kolkata"),new Artist("Tanmoy","Kolkata"))),
                  new Artist("Linkin Park","California",Stream.of(new Artist("Chester Bennington","California"),new Artist("Dave Farrell","California"), new Artist("Mike Shinoda","California"),new Artist("Rob Bourdon","California"),new Artist("Brad Delson","California"))),
                  new Artist("Cactus","Kolkata",Stream.of(new Artist("Sidhartha Sankar","Kolkata"),new Artist("Dibyendu Mukherjee","Kolkata"), new Artist("Ritaprabha","Kolkata"),new Artist("Sudipto","Kolkata"),new Artist("Mainak","Kolkata"))),
                  new Artist("Backstreet Boys","Florida",Stream.of(new Artist("A. J. McLean","Florida"),new Artist("Howie D.","Florida"),new Artist("Nick Carter","Florida"), new Artist("Kevin Richardson","Florida"), new Artist("Brian Littrell","Florida"))),
                  new Artist("Prohori","Kolkata",Stream.of(new Artist("Pritam","Kolkata"))));
  </code></pre>
  
  <p>We want to have no of single entity from "Kolkata". Using external iteration we can have following solution:</p>
  
  <pre><code>int totalMembers = 0;
  for (Artist artist : artists) {
      if(artist.getOrigin().equals("Kolkata") {
          Stream&lt;Artist&gt; members = artist.getMembers();
          totalMembers += members.count();
      }
  } 
  </code></pre>
  
  <p>What will be the solution for internal iteration using <code>stream()</code> and <code>flatMap()</code> with the help of Lambda-Expression ? </p>
  
  <p>I have thought of a solution but may be it is incorrect. </p>
  
  <pre><code>int totalMember = artists.stream()
                           .filter(d -&gt; d.getOrigin().equals("Kolkata"))
                           .flatMap(e -&gt; e.getMembers())
                           .map(f -&gt; 1).reduce(0, (a, b) -&gt; a + b);
  </code></pre>