title: Java8 stream: Iterate over nested list
body: <p>Trying to get my head round the Java 8 streams syntax with a simple example. Had a look at the other similar questions on this topic, but could not find any solutions that would match my example and would work for me.</p>
  <p>I have a class as follow</p>
  <pre><code>import java.util.List;
  
  public class Car {
      private String model;
      private String make;
      private String carName;
      private List&lt;Specification&gt; specification;
  
  
      public Car(String model, String make, String carName, List&lt;Specification&gt; specification) {
          this.model = model;
          this.make = make;
          this.carName = carName;
          this.specification = specification;
      }
  
      public String getModel() {
          return model;
      }
  
      public String getMake() {
          return make;
      }
  
      public String getCarName() {
          return carName;
      }
  
      public List&lt;Specification&gt; getSpecification() {
          return specification;
      }
  }
  
  
  public class Specification {
      private String name;
      private String value;
  
      public String getName() {
          return name;
      }
  
      public String getValue() {
          return value;
      }
  
      public Specification(String name, String value) {
          this.name = name;
          this.value = value;
      }
  }
  
  </code></pre>
  <p>And I have the main method</p>
  <pre><code>import java.io.IOException;
  import java.util.ArrayList;
  import java.util.List;
  
  public class RegisterCar {
  
      public static void main(String[] args) throws IOException {
  
          List&lt;String&gt; carNames = new ArrayList&lt;&gt;();
          carNames.add(&quot;Audi&quot;);
          carNames.add(&quot;BMW&quot;);
          carNames.add(&quot;Toyota&quot;);
         
          List&lt;String&gt; colour = new ArrayList&lt;&gt;();
          colour.add(&quot;red&quot;);
          colour.add(&quot;white&quot;);
      }
  }
  
  </code></pre>
  <p>I want to create a list of car object with each item of carNames. But if the car name is 'Audi' or 'BMW' it should create only one object of the class as follow</p>
  <pre><code>        List&lt;Car&gt; carList = new ArrayList&lt;&gt;();
          
          Specification musicSystem = new Specification(&quot;MusicSysten&quot; , &quot;present&quot;);
          
          List&lt;Specification&gt; specList= new ArrayList&lt;&gt;();
          specList.add(musicSystem);
          
          carList.add(new Car(&quot;Q5&quot;, &quot;2020&quot;, &quot;Audi&quot;, specList));
          carList.add(new Car(&quot;X2&quot;, &quot;2020&quot;, &quot;BMW&quot;, specList));
  
  
  </code></pre>
  <p>But if the item is 'Toyota' then it should create two object each for each color.</p>
  <pre><code> List&lt;Specification&gt; specListRed= new ArrayList&lt;&gt;();
          specListRed.add(musicSystem);
          specListRed.add(redColor);
  
          List&lt;Specification&gt; specListWhite= new ArrayList&lt;&gt;();
          specListWhite.add(musicSystem);
          specListWhite.add(whiteColor);
  
          carList.add(new Car(&quot;Camry&quot;, &quot;2020&quot;, &quot;Toyota&quot;, specListRed));
          carList.add(new Car(&quot;Camry&quot;, &quot;2020&quot;, &quot;Toyota&quot;, specListWhite));
  </code></pre>
  <p>I am trying to write a common method for creating object and adding to a list. I have tried something like this, but I won't create correct two object for Toyota.</p>
  <pre><code>carList =  carNames.stream()
                  .map(carName -&gt; new Car(model, make, carName, specList))
                  .collect(Collectors.toList());
  </code></pre>
  <p>please suggest how can I achieve this with streams.</p>