Java DoubleStream sum() Method

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

Table of Contents

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

Introduction

The sum() method is a terminal operation that returns the sum of elements in a DoubleStream. This method is particularly useful when you need to quickly calculate the total sum of a sequence of double values.

sum() Method Syntax

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

double 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 allows you to quickly calculate the total sum of all elements in a DoubleStream. It is a terminal operation, meaning it consumes the stream and produces a single result.

Examples

Basic Usage

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

Example

import java.util.stream.DoubleStream;

public class SumExample {
    public static void main(String[] args) {
        DoubleStream doubleStream = DoubleStream.of(1.1, 2.2, 3.3, 4.4, 5.5);

        // Calculate the sum of the elements in the stream
        double sum = doubleStream.sum();

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

Output:

Sum: 16.5

Using sum() with Other Stream Operations

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

Example

import java.util.stream.DoubleStream;

public class SumWithOtherOperationsExample {
    public static void main(String[] args) {
        DoubleStream doubleStream = DoubleStream.of(1.0, 2.0, 3.0, 4.0, 5.0);

        // Filter the elements and calculate the sum of the remaining elements
        double sum = doubleStream.filter(n -> n > 2.0).sum();

        // Print the sum
        System.out.println("Sum of elements greater than 2.0: " + sum);
    }
}

Output:

Sum of elements greater than 2.0: 12.0

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.DoubleStream;

public class TotalSalesExample {
    public static void main(String[] args) {
        DoubleStream salesAmounts = DoubleStream.of(250.75, 320.50, 120.00, 450.25, 600.00);

        // Calculate the total sales
        double totalSales = salesAmounts.sum();

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

Output:

Total Sales: 1741.5

Conclusion

The DoubleStream.sum() method is used to calculate the sum of elements in a stream. This method is particularly useful for scenarios where you need to aggregate the elements of a stream to get their total sum. By understanding and using this method, you can efficiently manage and process streams of double values in your Java applications.

Comments