

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find average of a list in Java
To find average of a list in Java, the code is as follows -
Example
import java.util.*; public class Demo { public static void main(String []args){ List<Integer> list = Arrays.asList(10, 20, 50, 100, 130, 150, 200, 250, 500); IntSummaryStatistics summaryStats = list.stream() .mapToInt((a) -> a) .summaryStatistics(); System.out.println("Average of a List = "+summaryStats.getAverage()); } }
Output
Average of a List = 156.66666666666666
Let us now see another example -
Example
import java.util.*; public class Demo { public static void main(String []args){ List<Integer> list = Arrays.asList(10, 20, 50, 100, 130, 150, 200, 250, 500); int sum = 0; for (int i : list) { sum+=i; } if(list.isEmpty()){ System.out.println("Empty list!"); } else { System.out.println("Average = " + sum/(float)list.size()); } } }
Output
Average = 156.66667
- Related Questions & Answers
- Find average of a list in python?
- Python – Average digits count in a List
- Average of each n-length consecutive segment in a Python list
- Java program to find the average of given numbers using arrays
- Golang Program to Calculate the Average of Numbers in a Given List
- Get the Average of Average in a single MySQL row?
- Java Program to Find a Sublist in a List
- How do I find the size of a Java list?
- Create a MySQL function and find the average of values in a column
- C# Program to find the average of a sequence of numeric values
- Find maximum average subarray of k length in C++
- 8086 program to find average of n numbers
- Find consecutive elements average JavaScript
- IntStream average() method in Java
- LongStream average() method in Java
Advertisements