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

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);

    }

}

Output:

Average of Marks: OptionalDouble[55.0]

Example 2. Java 8 code to find average of marks using Array

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);

    }


}

Output:

Average of Marks: OptionalDouble[52.9]

Example 3. Java 8 code to find average of marks using Array with Scanner

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);

    }


}

Output:

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]

Java 8 programs to calculate average of marks...!!! Share on X

Do you like this Post? – then check my other helpful posts:

Other Useful References:

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester.

    He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    View all posts