How to use non-final variable inside Java8 streams/filters?
<p>In my use case I would like to update the value of a variable and reference the same in next iteration in streams. </p>

<p>But the java compiler is giving me error. Here is my code</p>

<pre><code>static String convertList(        
  List&lt;Map.Entry&lt;String, String&gt;&gt; map,         
  String delimiter,            
  long maxLength        
) {          
    long currentLength = 0L;          
    return map.stream()
    .map(e-&gt;e.getKey() + "=" + e.getValue())        
    .filter(p-&gt;{                
      long pLength = p.getBytes(StandardCharsets.UTF_8).length;        
      currentLength = currentLength + pLength;        
      if (currentLength &lt;= maxLength) {         
        return true;        
      } else {
        return false;        
      }
    })
  .collect(Collectors.joining(delimiter));        
}
</code></pre>

<p>I am trying to get the values from the list to a string until the length [till this iteration] &lt;= maxlength</p>

<p>could someone help me fix this? I am getting <code>Local variables referenced from a lambda expression must be final or effectively final</code> error.</p>