We would like to know how to use class field variable to save calculation for a method reference.
/* w ww . j a va 2 s . com*/ import java.util.stream.LongStream; public class Main { public static void main(String...args){ Accumulator accumulator = new Accumulator(); LongStream.rangeClosed(1, 100).forEach(accumulator::add); System.out.println(accumulator.total); } } class Accumulator { long total = 0; public void add(long value) { total += value; } }
The code above generates the following result.