BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Calculate Average of List in Java

Last Updated: June 13, 2024 by Chaitanya Singh | Filed Under: java

In this tutorial, you will learn how to calculate average of a List in Java. This can be done on ArrayList of numbers such as list of Integer, Double, Float, Long type etc. There are several ways to do this, in this guide, we will see following two approaches:

  • Using For loop
  • Using Java Streams

1. Using For-Loop

In this example, we have an ArrayList of Integer type, and we are using for loop to calculate the average of ArrayList.

import java.util.ArrayList;

public class AverageCalculator {
public static void main(String[] args) {
// Creating an ArrayList of Integer type
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(10);
numbers.add(20);
numbers.add(30);
numbers.add(40);
numbers.add(50);

// Calling user defined method to calculate the average
double average = calculateAverage(numbers);

// Print the average
System.out.println("The average is: " + average);
}

public static double calculateAverage(ArrayList<Integer> list) {
// If list is null or empty return 0
if (list == null || list.isEmpty()) {
return 0;
}

// else iterate the list using for loop
// and add each element to variable sum
double sum = 0;
for (int number : list) {
sum += number;
}

// Divide sum of elements by number of elements
// to calculate the average of ArrayList
return sum / list.size();
}
}

2. Using Java Streams (Java 8 and Later)

Java 8 introduced the Stream API, which can be used for this purpose. To learn more about Streams, refer Java Streams Tutorial.

import java.util.ArrayList;
import java.util.List;

public class AverageCalculator {
public static void main(String[] args) {
// Creating an ArrayList of Integer type
List<Integer> numbers = new ArrayList<>();
numbers.add(10);
numbers.add(20);
numbers.add(30);
numbers.add(40);
numbers.add(50);

// Calculate the average using streams
double average = calculateAverage(numbers);

// Print the average
System.out.println("The average is: " + average);
}

// In this method, we are converting list to stream using stream()
// method then converting elements from Integer to int using mapToInt()
// and finally calling average() method to calculate the average of elements
public static double calculateAverage(List<Integer> list) {
return list.stream()
.mapToInt(Integer::intValue)
.average()
.orElse(0.0);
}
}
❮ Java ArrayList

Top Related Articles:

  1. Convert JSON Array to ArrayList in Java
  2. Java 8 Interface Changes – default method and static method
  3. Java 9 – @SafeVarargs Annotation (with examples)
  4. Java 8 Stream Tutorial
  5. How to Convert an array to ArrayList in java

Tags: Java-ArrayList

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap