LazyEvaluation

The Style of only evaluating what is needed. oro only evaluating a until that value is needed.

Avoid repeated or strict evaluation.

Given

List<Integer> numbers = Arrays.asList(1,2,3);

We have a lambda that relies on the external array factor

int[] factor = new int[] { 2 };
Steam<Integer> strm = numbers.stream().map(e -> e * factor[0]);

factor[0] = 0
strm.forEach(System.out::println)

This makes the lambda not pure; it relies on external elements that can change.

While we initially expected the printed output to be 2, 4, 6 we’ll be getting 0,0,0 This is because the lazy evaluation happens after the change.

Edit this page on GitHub

Links to this note