1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# Sum Example-1 Integer sum = Arrays.stream(string.split(" ")) .filter((s) -> s.matches("\\d+")) .mapToInt(Integer::valueOf) .sum(); # Sum Example-2 List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5); Integer sum = integers.stream() .mapToInt(Integer::intValue) .sum(); # Sum Example-3 Integer sum = items.stream() .map(x -> x.getPrice()) .collect(Collectors.summingInt(Integer::intValue)); # Sum Example-4 Integer sum = items.stream() .map(item -> item.getPrice()) .reduce(0, (a, b) -> a + b); # Sum Example-5 Integer sum = items.stream() .map(x -> x.getPrice()) .reduce(0, ArithmeticUtils::add); # Sum Example-6 Integer sum = items.stream() .map(x -> x.getPrice()) .reduce(0, Integer::sum);intro # Sum Example-7 Integer sum = integers.stream() .reduce(0, (a, b) -> a + b); |