0 votes
333 views
in Java by
How will you find the average of numbers in a list by using Java 8?

1 Answer

0 votes
by (5k points)
See the following running example to find the average of 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 AverageOfAllNumbersInList {

    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("Average of numbers : " + intSummaryStatistics.getAverage());
    }

}

Output:- Average of numbers : 3.8333333333333335

Share:- Whatsapp Facebook Facebook


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

Categories

...