Java Online Compiler
Example: Sum of Array Elements using Java 8 Stream API (sum()) in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// Sum of Array Elements using Java 8 Stream API (sum()) import java.util.Arrays; // Required for Arrays.stream() // Main class containing the entry point of the program public class Main { public static void main(String[] args) { // Step 1: Define the array of integers int[] numbers = {10, 20, 30, 40, 50}; // Step 2: Convert the array to an IntStream and calculate the sum // Arrays.stream(int[]) creates an IntStream // sum() is a terminal operation that calculates the sum of elements int sum = Arrays.stream(numbers).sum(); // Step 3: Print the calculated sum System.out.println("Sum of array elements (Stream API sum()): " + sum); } }
Output
Clear
ADVERTISEMENTS