Thursday, March 24, 2016

Java 8 - using stream API to add numbers in an array

Integer[] nums = {1,2,3,4,5,6,7};
List<Integer> numbers = Arrays.asList(nums);
int sum = numbers.stream().reduce(0, (x, y) -> x+y);
System.out.println(sum);
sum = numbers.stream().mapToInt(i -> i).sum();
System.out.println(sum);

No comments:

Post a Comment