Java IntStream sum() Method

The sum() method in Java, part of the java.util.stream.IntStream interface, is used to calculate the sum of elements in the stream. This method is useful when you need to aggregate the total of all elements in an IntStream.

Table of Contents

  1. Introduction
  2. sum() Method Syntax
  3. Understanding sum()
  4. Examples
    • Basic Usage
    • Using sum() with Filtered Streams
  5. Real-World Use Case
  6. Conclusion

Introduction

The sum() method is a terminal operation that returns the sum of elements in the stream. If the stream is empty, the sum will be 0.

sum() Method Syntax

The syntax for the sum() method is as follows:

int sum()

Parameters:

  • This method does not take any parameters.

Returns:

  • The sum of elements in the stream.

Throws:

  • This method does not throw any exceptions.

Understanding sum()

The sum() method processes the elements of the stream and returns their total sum. This method is particularly useful for aggregating numeric data.

Examples

Basic Usage

To demonstrate the basic usage of sum(), we will create an IntStream and use sum() to calculate the total sum of its elements.

Example

import java.util.stream.IntStream;

public class SumExample {
    public static void main(String[] args) {
        IntStream intStream = IntStream.of(1, 2, 3, 4, 5);

        // Use sum() to calculate the total sum of elements in the stream
        int totalSum = intStream.sum();

        // Print the total sum
        System.out.println("Total Sum: " + totalSum);
    }
}

Output:

Total Sum: 15

Using sum() with Filtered Streams

This example shows how to use sum() in combination with other stream operations, such as filtering.

Example

import java.util.stream.IntStream;

public class SumWithFilterExample {
    public static void main(String[] args) {
        IntStream intStream = IntStream.range(1, 10);

        // Filter even numbers and calculate their sum
        int sumOfEvens = intStream.filter(n -> n % 2 == 0).sum();

        // Print the sum of even numbers
        System.out.println("Sum of Even Numbers: " + sumOfEvens);
    }
}

Output:

Sum of Even Numbers: 20

Real-World Use Case

Calculating Total Sales

In real-world applications, the sum() method can be used to calculate the total sales from a stream of sales amounts.

Example

import java.util.stream.IntStream;

public class TotalSalesExample {
    public static void main(String[] args) {
        IntStream sales = IntStream.of(100, 200, 300, 400, 500);

        // Use sum() to calculate the total sales
        int totalSales = sales.sum();

        // Print the total sales
        System.out.println("Total Sales: " + totalSales);
    }
}

Output:

Total Sales: 1500

Conclusion

The IntStream.sum() method is used to calculate the sum of elements in a stream. This method is particularly useful for aggregating numeric data. By understanding and using this method, you can efficiently manage and process streams of integer values in your Java applications, performing necessary calculations and aggregations as needed.

Comments