- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 Articles
- 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
- Golang Program to Calculate the Average of Numbers in a Given List
- Java Program to Find a Sublist in a List
- Java program to find the average of given numbers using arrays
- Find the Extreme elements in a List in Java
- Java program to calculate the average of numbers in Java
- How can I find elements in a Java List?
- How do I find the size of a Java list?
- IntStream average() method in Java
- LongStream average() method in Java
- DoubleStream average() method in Java
- How to Average Filtered Cells/List in Excel?
- How to find the index of given element of a Java List?

Advertisements