Java Program to Find Average of Two Lists Last Updated : 13 Dec, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 ListsThere are three methods to find the average of the two lists are mentioned below: Using simple for loop +len() and + OperatorUsing the sum(), len() and + OperatorsUsing sum() + len() + chain()1. Using simple for loop +len() and + OperatorBelow 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); } } OutputAverage: 3.5 2. Using the sum(), len(), and + operatorsBelow 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); } } OutputAverage: 3.53. 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); } } OutputAverage: 5.5 Comment More infoAdvertise with us Next Article Java Program to Concatenate Two List S subramanyasmgm Follow Improve Article Tags : Java Java Programs Geeks Premier League Geeks Premier League 2023 Practice Tags : Java Similar Reads Java Program to Concatenate Two List Concatenating two lists means merging two lists into a single list. Consider the given lists: LIST 1LIST 2LIST AFTER CONCATENATION There are several methods to perform concatenation operation: Using addAll() methodUsing streamUsing union() Method 1: Using addAll() method Syntax: addAll ( list name ) 3 min read Java Program For Union And Intersection Of Two Linked Lists Write a java program for a given two Linked Lists, create union and intersection lists that contain union and intersection of the elements present in the given lists. The order of elements in output lists doesn't matter.Example: Input:List1: 10->15->4->20List2: 8->4->2->10Output:In 10 min read Java Program to Combine Two List by Alternatively Taking Elements A list is an ordered sequence of elements stored together to form a collection. A list can contain duplicate as well as null entries. A list allows us to perform index-based operations, that is additions, deletions, manipulations, and positional access. Java provides an in-built interface <<ja 6 min read Java Program for Mean of range in array Given an array of n integers. You are given q queries. Write a program to print the floor value of mean in range l to r for each query in a new line. Examples : Input : arr[] = {1, 2, 3, 4, 5} q = 3 0 2 1 3 0 4 Output : 2 3 3 Here for 0 to 2 (1 + 2 + 3) / 3 = 2 Input : arr[] = {6, 7, 8, 10} q = 2 0 4 min read Java Program to Merge Two Sorted Linked Lists in New List We are given two sorted List and our goal is to merge these two lists into a new list. For that, we have to write one function which will take two List as an argument which is sorted in increasing order. This function will Merge these two List into one List in increasing order. Input List 1 : 1-> 4 min read Java Program to Find Common Elements Between Two Arrays Given two arrays and our task is to find their common elements. Examples:Input: Array1 = ["Article", "for", "Geeks", "for", "Geeks"], Array2 = ["Article", "Geeks", "Geeks"]Output: [Article, Geeks]Input: Array1 = ["a", "b", "c", "d", "e", "f"], Array2 = ["b", "d", "e", "h", "g", "c"]Output: [b, c, d, 4 min read Like