title: Lambda Collector throwing java.lang.ArrayIndexOutOfBoundsException
body: <p>Lambda expression intermittently throws ArrayIndexOutOfBoundsException in my Impl.java class.</p>
  
  <p>Stacktrace:</p>
  
  <pre><code>java.lang.ArrayIndexOutOfBoundsException: 1 java.lang.ArrayIndexOutOfBoundsException: 1
      at Impl.lambda$retrieve$0(Impl.java:124) ~[classes/:?]
      at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193) ~[?:1.8.0_181]
      at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1382) ~[?:1.8.0_181]
      at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481) ~[?:1.8.0_181]
      at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471) ~[?:1.8.0_181]
      at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708) ~[?:1.8.0_181]
      at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[?:1.8.0_181]
      at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499) ~[?:1.8.0_181]
      at Impl.retrieve(Impl.java:125) ~[classes/:?]
  </code></pre>
  
  <p>I've written a sample program simulating the scenario. But, in the sample program, it is NOT reproducible. However, can someone figure out any problem with the following code?</p>
  
  <p>I've tried with empty/non-empty input. When I make numberStr to throw ArrayIndexOutOfBoundsException, the stack trace appears different:</p>
  
  <pre><code>Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
      at LambdaTest.lambda$0(LambdaTest.java:13)
      at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
      at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1382)
      at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
      at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
      at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
      at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
      at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
      at LambdaTest.main(LambdaTest.java:14)
  </code></pre>
  
  <p>Sample program:</p>
  
  <pre><code>import java.util.List;
  import java.util.stream.Collectors;
  
  import org.apache.commons.math3.util.Pair;
  
  import com.google.common.collect.Lists;
  
  public class LambdaTest {
  
      public static void main(String[] args) {
          String[] numberStr = { "one", "two", "three" };
          List&lt;Pair&lt;String, SampleEnum&gt;&gt; result = getInputPairs().stream()
                  .map(entry -&gt; new Pair&lt;String, SampleEnum&gt;(numberStr[entry.getFirst()], entry.getSecond()))
                  .collect(Collectors.toList()); // &lt;=== similar to Impl.java:125
          System.out.println("Lambda tested." + result.toString());
      }
  
      private static List&lt;Pair&lt;Integer, SampleEnum&gt;&gt; getInputPairs() {
          List&lt;Pair&lt;Integer, SampleEnum&gt;&gt; pairs = Lists.newArrayList();
          //pairs.add(new Pair&lt;Integer, SampleEnum&gt;(0, SampleEnum.ONE_AND_ONLY));
          return pairs;
      }
  
  }
  
  enum SampleEnum {
      ONE_AND_ONLY
  }
  </code></pre>
  
  <p>In what cases, can the expression fail? and how to fix it?</p>