In this example, we will see “How to calculate average of marks 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 calculate average of marks...!!! Share on X
Example 1. Java 8 code to find average of marks 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 marks using list in Java 8 * * @author D.V * */ public class Calculate_Average_Marks_List_Of_Integers_Java8 { public static void main(String[] args) { List <Integer> listOfMarks = Arrays.asList(20, 50, 99, 12, 94); OptionalDouble averageOfMarks = listOfMarks .stream() .mapToInt(number -> number.intValue()) .average(); System.out.println("Average of Marks: " + averageOfMarks); } } |
1 | Average of Marks: OptionalDouble[55.0] |
Example 2. Java 8 code to find average of marks 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 marks using Array in Java 8 * * @author D.V * */ public class Calculate_Average_Marks_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 streamOfMarks = Arrays.stream(arr); OptionalDouble averageOfMarks = streamOfMarks.average(); System.out.println("Average of Marks: " + averageOfMarks); } } |
1 | Average of Marks: OptionalDouble[52.9] |
Example 3. Java 8 code to find average of marks 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 marks using Array with Scanner in Java 8 * * @author D.V * */ public class Calculate_Average_Marks_Array_Of_Integers_Using_Scanner_Java8 { private static Scanner scanner; public static void main(String[] args) { System.out.println("Enter how many marks 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 marks now\n"); for (int i = 0; i < number; i++) { System.out.println("Enter marks of subject " + (i + 1) + ":"); arr[i] = scanner.nextInt(); } IntStream streamOfMarks = Arrays.stream(arr); OptionalDouble averageOfMarks = streamOfMarks.average(); System.out.println("\nAverage of Marks: " + averageOfMarks); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | Enter how many marks you want to use to calculate the average 5 Enter marks now Enter marks of subject 1: 6 Enter marks of subject 2: 90 Enter marks of subject 3: 55 Enter marks of subject 4: 71 Enter marks of subject 5: 11 Average of Marks: OptionalDouble[46.6] |
Do you like this Post? – then check my other helpful posts: