Java Stream How to - Enclose local variable in lambda








Question

We would like to know how to enclose local variable in lambda.

Answer

import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
//ww w .  j  a v  a2 s . c o m
public class Main {
    public static void main(String[] args) {
        List<Integer> l = Arrays.asList(1, 3, 2, 4, 7, 8, 9, 6, 5);
        int variableOutside = 50;
        Consumer<Integer> consumer = i -> { //Type of i can be implicitly known
            int variableInside = 70; 
            variableInside++;
            System.out.printf("%d plus %d plus %d equals %d\n", variableOutside, variableInside, i, variableOutside+variableInside+i);
        };
        //variableOutside = 100; //Must be effectively final

        l.forEach(consumer);
    }
}

The code above generates the following result.