title: Foreach loop with double condition in java8 using lambda
body: <p>I have an exercise with this description:</p>
  
  <pre><code>1122 produces a sum of 3 (1 + 2) because the first digit (1) matches the second digit and the third digit (2) matches the fourth digit.
  1111 produces 4 because each digit (all 1) matches the next.
  1234 produces 0 because no digit matches the next.
  91212129 produces 9 because the only digit that matches the next one is the last digit, 9.
  </code></pre>
  
  <p>I have written the following code:</p>
  
  <pre><code>String inputString = "1111"; // taking this number as example
  
  int sum = 0;
  for (int i = 0; i &lt; inputString.length() - 1; i++) {
      if (inputString.charAt(i) == inputString.charAt(i + 1)) {
          sum += Integer.parseInt(String.valueOf(inputString.charAt(i)));
      }
      if (i + 2 == inputString.length() - 1) {
          if (inputString.charAt(i + 2) == inputString.charAt(0)) {
              sum += Integer.parseInt(String.valueOf(inputString.charAt(i + 2)));
          }
      }
  }
  </code></pre>
  
  <p>The result of <code>sum</code> is 4, which is correct.</p>
  
  <p>Now I'm trying to write the same one in Java8 using lambda but I can't figure out how to get the last condition inside my stream.</p>
  
  <p>This is how far I got:</p>
  
  <pre><code>Integer sum = IntStream.range(0, (inputString.length() - 1)).boxed()
                  .filter(j -&gt; inputString.charAt(j) == inputString.charAt(j + 1))
                  .mapToInt(i -&gt; Integer.parseInt(String.valueOf(inputString.charAt(i)))).sum();
  </code></pre>