0 votes
356 views
in Java by
How will you find the sum of all numbers in a list by using Java 8?

1 Answer

0 votes
by (5k points)
See the bellow running code to print the sum of all numbers in a list by using java 8:-

package com.java8.list;

import java.util.Arrays;
import java.util.IntSummaryStatistics;
import java.util.List;

public class SumOfAllNumbersInList {

    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(3, 2, 3, 7, 3, 5);
        IntSummaryStatistics intSummaryStatistics = numbers.stream().mapToInt((x) -> x).summaryStatistics();
        System.out.println("Sum of numbers : " + intSummaryStatistics.getSum());
    }

}

Output:-Sum of numbers : 23

Share:- Whatsapp Facebook Facebook


Welcome to Developerhelpway Q&A, where you can ask questions and receive answers from other members of the community.

Categories

...