In this example, we will see “How to calculate average of N numbers in Java 8?”. To achieve that, we are going to use Stream API introduced in Java 8 to find average and we will see it through several different ways.
Java 8 programs to find average of n numbers...!!! Share on X
In this tutorial, we are going to cover below topics:
- How to calculate average of N numbers?
- Different ways of calculating the average.
Let’s begin:
1. How to calculate average of N numbers?
It’s very easy to find out the average. Average has been calculated as sum of n numbers divided by n.
In order to calculate average, add all the numbers and divide them by count of numbers.
Let’s take an example,
There are following numbers available: 7, 10, 81, 33, 69
And, we need to find the average of these numbers, what we need to do is:
a. First add all the numbers: (7+10+81+33+69) = 200
b. Count the numbers: (7,10,81,33,69) = 5
c. Find average by dividing a from b: 200 / 5 = 40
2. Different ways of calculating the average.
Example 1. Java 8 code to find average of numbers using List
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | import java.util.Arrays; import java.util.List; import java.util.OptionalDouble; /** * To calculate average of numbers using list in Java 8 * * @author D.V * */ public class Calculate_Average_Numbers_List_Of_Integers_Java8 { public static void main(String[] args) { List <Integer> listOfIntegers = Arrays.asList(20, 50, 99, 12, 94); OptionalDouble average = listOfIntegers .stream() .mapToInt(number -> number.intValue()) .average(); System.out.println("Average of Numbers: " + average); } } |
Output:
1 | Average of Numbers: OptionalDouble[55.0] |
Example 2. Java 8 code to find average of numbers using Array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | import java.util.Arrays; import java.util.OptionalDouble; import java.util.stream.IntStream; /** * To calculate average of numbers using Array in Java 8 * * @author D.V * */ public class Calculate_Average_Numbers_Array_Of_Integers_Java8 { public static void main(String[] args) { int[] arr = new int[10]; arr[0] = 20; arr[1] = 50; arr[2] = 99; arr[3] = 12; arr[4] = 94; arr[5] = 22; arr[6] = 43; arr[7] = 56; arr[8] = 55; arr[9] = 78; IntStream streamOfNumbers = Arrays.stream(arr); OptionalDouble averageOfNumbers = streamOfNumbers.average(); System.out.println("Average of Numbers: " + averageOfNumbers); } } |
Output:
1 | Average of Numbers: OptionalDouble[52.9] |
Example 3. Java 8 code to find average of numbers using Array with Scanner
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | import java.util.Arrays; import java.util.OptionalDouble; import java.util.Scanner; import java.util.stream.IntStream; /** * To calculate average of numbers using Array with Scanner in Java 8 * * @author D.V * */ public class Calculate_Average_Numbers_Array_Of_Integers_Using_Scanner_Java8 { private static Scanner scanner; public static void main(String[] args) { System.out.println("Enter how many numbers you want to use to calculate the average"); scanner = new Scanner(System.in); int number = scanner.nextInt(); int[] arr = new int[number]; System.out.println("\nEnter numbers now\n"); for (int i = 0; i < number; i++) { System.out.println("Enter number " + (i + 1) + ":"); arr[i] = scanner.nextInt(); } IntStream streamOfNumbers = Arrays.stream(arr); OptionalDouble averageOfNumbers = streamOfNumbers.average(); System.out.println("\nAverage of Numbers: " + averageOfNumbers); } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | Enter how many numbers you want to use to calculate the average 5 Enter numbers now Enter number of subject 1: 6 Enter number of subject 2: 90 Enter number of subject 3: 55 Enter number of subject 4: 71 Enter number of subject 5: 11 Average of Numbers: OptionalDouble[46.6] |
Java 8 programs to find average of n numbers...!!! Share on X
Do you like this Post? – then check my other helpful posts: