Share code snippets.

Add-up-integers-using-Java8-lambdas

Created 1 year ago by anonymous
Add up integers using Java 8 lambdas
Arrays.asList(1,2,3,4).stream().mapToInt(i -> i.intValue()).sum();
Arrays.asList(1,2,3,4).stream().mapToInt(Integer::intValue).sum();
Arrays.asList(1,2,3,4).stream().collect(Collectors.summingInt(Integer::intValue));

// parallel streams
LongAdder a = new LongAdder();
Arrays.asList(1,2,3,4).parallelStream().forEach(a::add);
sum = a.intValue();


int sum = Arrays.asList(1,2,3,4).stream().reduce(0, (x,y) -> x+y)