Java DoubleStream

Introduction

In Java, DoubleStream is a sequence of primitive double elements supporting sequential and parallel aggregate operations. It is part of the java.util.stream package and provides a convenient way to work with sequences of double values.

Table of Contents

  1. What is DoubleStream?
  2. Creating a DoubleStream
  3. Common Operations
  4. Examples of DoubleStream
  5. Real-World Use Case
  6. Conclusion

1. What is DoubleStream?

DoubleStream is a specialized stream for working with double values, providing methods for processing elements, performing calculations, and generating statistics.

2. Creating a DoubleStream

You can create a DoubleStream in several ways:

  • From an array: DoubleStream.of(double... values)
  • Using Arrays.stream(double[] array)
  • From a collection using stream().mapToDouble()
  • Using DoubleStream.generate() or DoubleStream.iterate()

3. Common Operations

Some common operations on DoubleStream include:

  • filter(): Filters elements based on a condition.
  • map(): Transforms elements.
  • average(): Calculates the average of the elements.
  • sum(): Computes the sum of the elements.
  • min()/max(): Finds the minimum or maximum element.
  • sorted(): Sorts the elements.
  • forEach(): Performs an action for each element.

4. Examples of DoubleStream

Example 1: Creating and Summing a DoubleStream

This example demonstrates how to create a DoubleStream from a set of double values and calculate their sum.

import java.util.stream.DoubleStream;

public class SumExample {
    public static void main(String[] args) {
        double sum = DoubleStream.of(1.2, 2.5, 3.7)
                                 .sum();
        System.out.println("Sum: " + sum);
    }
}

Output:

Sum: 7.4

Example 2: Filtering and Averaging

In this example, we filter the stream to include only values greater than 2.0 and then calculate the average of the remaining values.

import java.util.OptionalDouble;
import java.util.stream.DoubleStream;

public class FilterAverageExample {
    public static void main(String[] args) {
        OptionalDouble average = DoubleStream.of(1.2, 2.5, 3.7, 4.1)
                                             .filter(n -> n > 2.0)
                                             .average();
        System.out.println("Average: " + average.orElse(0.0));
    }
}

Output:

Average: 3.433333333333333

Example 3: Mapping and Sorting

This example shows how to map each element of a DoubleStream to its square and then sort the resulting values.

import java.util.stream.DoubleStream;

public class MapSortExample {
    public static void main(String[] args) {
        DoubleStream.of(1.2, 2.5, 3.7)
                    .map(n -> n * n)
                    .sorted()
                    .forEach(System.out::println);
    }
}

Output:

1.44
6.25
13.69

5. Real-World Use Case: Calculating Standard Deviation

This example illustrates how to calculate the standard deviation of a set of double values using DoubleStream.

import java.util.stream.DoubleStream;

public class StandardDeviationExample {
    public static void main(String[] args) {
        double[] numbers = {1.2, 2.5, 3.7, 4.1};
        double average = DoubleStream.of(numbers).average().orElse(0.0);
        double variance = DoubleStream.of(numbers)
                                      .map(n -> Math.pow(n - average, 2))
                                      .average()
                                      .orElse(0.0);
        double stdDev = Math.sqrt(variance);
        System.out.println("Standard Deviation: " + stdDev);
    }
}

Output:

Standard Deviation: 1.1078230586932

Conclusion

DoubleStream is used in Java for processing sequences of double values. It provides a range of operations for filtering, transforming, and aggregating data, making it particularly useful in numerical and scientific computations. Using DoubleStream can lead to cleaner and more efficient code, especially in data processing contexts.

Comments