title: How to map elements of the list to their indices using Java 8 streams?
body: <p>Having a list of strings, I need to construct a list of objects which are effectively pairs <code>(string, its position in the list)</code>. Currently I have such code using google collections:</p>
  
  <pre><code>public Robots(List&lt;String&gt; names) {
      ImmutableList.Builder&lt;Robot&gt; builder = ImmutableList.builder();
      for (int i = 0; i &lt; names.size(); i++) {
          builder.add(new Robot(i, names.get(i)));
      }
      this.list = builder.build();
  }
  </code></pre>
  
  <p>I would like to do this using Java 8 streams. If there was no index, I could just do:</p>
  
  <pre><code>public Robots(List&lt;String&gt; names) {
      this.list = names.stream()
              .map(Robot::new) // no index here
              .collect(collectingAndThen(
                      Collectors.toList(),
                      Collections::unmodifiableList
              ));
  }
  </code></pre>
  
  <p>To get the index, I would have to do something like this: </p>
  
  <pre><code>public Robots(List&lt;String&gt; names) {
      AtomicInteger integer = new AtomicInteger(0);
      this.list = names.stream()
              .map(string -&gt; new Robot(integer.getAndIncrement(), string))
              .collect(collectingAndThen(
                      Collectors.toList(),
                      Collections::unmodifiableList
              ));
  }
  </code></pre>
  
  <p>However, the documentation says that mapping function should be stateless, but the <code>AtomicInteger</code> is effectively its state.</p>
  
  <p>Is there a way to map elements of the sequential stream to their positions in the stream?</p>