Horje
Java Program to Find Average of Two Lists

To calculate the average of two lists in Java we first need to combine the two lists into one. we can do this using the addAll() method of the ArrayList class. Once you have combined the lists we can calculate the average by summing up all the elements in the combined list and dividing by the total number of factors.

Methods to Find Average of Two Lists

There are three methods to find the average of the two lists are mentioned below:

  • Using simple for loop +len() and + Operator
  • Using the sum(), len() and + Operators
  • Using sum() + len() + chain()

1. Using simple for loop +len() and + Operator

Below is the Implementation of the above method:

Java

// Java Program using the
// somple for loops len(), and + operators
import java.util.*; //imports all the util packages
 
// Driver Class
public class Main {
    // main function
    public static void main(String[] args)
    {
        // Creating two lists
        List<Integer> list1 = new ArrayList<Integer>();
        List<Integer> list2 = new ArrayList<Integer>();
        // Adding elements in the two lists
        list1.add(1);
        list1.add(2);
        list1.add(3);
        list2.add(4);
        list2.add(5);
        list2.add(6);
 
        double avg = 0;
        // Using for loops to find the average
        for (int i = 0; i < list1.size(); i++)
            avg += list1.get(i);
        for (int i = 0; i < list2.size(); i++)
            avg += list2.get(i);
 
        int l = list1.size() + list2.size();
 
        System.out.println("Average: " + avg / l);
    }
}

Output

Average: 3.5

2. Using the sum(), len(), and + operators

Below is the Implementation of the above method:

Java

// Java Program to Using the
// sum(), len(), and + operators
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
// Driver Class
public class Main {
    // main function
    public static void main(String[] args)
    {
        List<Integer> list1
            = new ArrayList<>(Arrays.asList(1, 2, 3));
        List<Integer> list2
            = new ArrayList<>(Arrays.asList(4, 5, 6));
 
        double average = ((double)list1.stream()
                          .mapToInt(Integer::intValue).sum()
                          + (double)list2.stream()
                          .mapToInt(Integer::intValue).sum())
                         / (list1.size() + list2.size());
 
        System.out.println("Average: " + average);
    }
}

Output

Average: 3.5



3. Using sum() + len() + chain()

Below is the Implementation of the above method:

Java

// Java Program to demonstrate
// Using sum() + len() + chain()
import java.io.*;
import java.util.*;
import java.util.stream.*;
 
// Driver Class
public class AverageOfTwoLists {
      // main function
    public static void main(String[] args) {
        List<Integer> list1 = Arrays.asList(1, 2, 3, 4, 5);
        List<Integer> list2 = Arrays.asList(6, 7, 8, 9, 10);
         
        double average = Stream.concat(list1.stream(), list2.stream())
                              .mapToInt(Integer::intValue)
                              .average()
                              .orElse(Double.NaN);
         
        System.out.println("Average: " + average);
    }
}

Output

Average: 5.5



Reffered: https://www.geeksforgeeks.org


Geeks Premier League

Related
Java Program For Printing 180 Degree Rotation of Simple Half Left Pyramid Java Program For Printing 180 Degree Rotation of Simple Half Left Pyramid
How to use a Web Browser How to use a Web Browser
Greater Than and Less Than Symbols in Maths | Meaning &amp; Examples Greater Than and Less Than Symbols in Maths | Meaning &amp; Examples
Word and Letter Counter using React Word and Letter Counter using React
JavaScript Program to Generate n-bit Gray Codes JavaScript Program to Generate n-bit Gray Codes

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
9