Java DoubleStream collect() Method

The collect() method in Java, part of the java.util.stream.DoubleStream interface, is a terminal operation that allows you to transform the elements of a stream into a different form, such as a collection. This method is highly versatile and can be used to perform mutable reduction operations on the elements of the stream.

Table of Contents

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

Introduction

The collect() method is a terminal operation that performs a mutable reduction operation on the elements of a DoubleStream using a Collector. This method is highly versatile and can be used to collect elements into various collections or to perform custom reduction operations.

collect() Method Syntax

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

<R> R collect(Supplier<R> supplier, ObjDoubleConsumer<R> accumulator, BiConsumer<R, R> combiner)

Parameters:

  • supplier: A function that provides a new result container.
  • accumulator: A function that folds an element into a result container.
  • combiner: A function that combines two result containers.

Returns:

  • The result of the collection operation.

Throws:

  • This method does not throw any exceptions.

Understanding collect()

The collect() method allows you to transform the elements of a DoubleStream into a different form. The method takes three arguments:

  • A supplier that provides a new result container.
  • An accumulator that folds elements into the result container.
  • A combiner that combines two result containers, typically used in parallel processing.

Examples

Basic Usage with BoxedStream

To demonstrate the basic usage of collect(), we will convert a DoubleStream into a List<Double> using a BoxedStream.

Example

import java.util.ArrayList;
import java.util.List;
import java.util.stream.DoubleStream;

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

        // Collect the elements of the DoubleStream into a List<Double>
        List<Double> doubleList = doubleStream.collect(
            ArrayList::new,
            ArrayList::add,
            ArrayList::addAll
        );

        System.out.println("Collected List: " + doubleList);
    }
}

Output:

Collected List: [1.1, 2.2, 3.3, 4.4, 5.5]

Using collect() with DoubleSummaryStatistics

This example shows how to use collect() to gather statistical information about the elements in a DoubleStream using DoubleSummaryStatistics.

Example

import java.util.DoubleSummaryStatistics;
import java.util.stream.DoubleStream;

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

        // Collect the elements of the DoubleStream into DoubleSummaryStatistics
        DoubleSummaryStatistics stats = doubleStream.collect(
            DoubleSummaryStatistics::new,
            DoubleSummaryStatistics::accept,
            DoubleSummaryStatistics::combine
        );

        System.out.println("Statistics: " + stats);
    }
}

Output:

Statistics: DoubleSummaryStatistics{count=5, sum=16.500000, min=1.100000, average=3.300000, max=5.500000}

Real-World Use Case

Collecting Sensor Data into a Custom Collection

In real-world applications, the collect() method can be used to collect sensor data into a custom collection for further analysis.

Example

import java.util.ArrayList;
import java.util.List;
import java.util.stream.DoubleStream;

public class SensorDataCollectionExample {
    static class SensorData {
        private final double value;

        SensorData(double value) {
            this.value = value;
        }

        @Override
        public String toString() {
            return "SensorData{" +
                    "value=" + value +
                    '}';
        }
    }

    public static void main(String[] args) {
        DoubleStream sensorValues = DoubleStream.of(25.3, 26.7, 23.8, 24.1, 25.9);

        // Collect the sensor values into a List<SensorData>
        List<SensorData> sensorDataList = sensorValues.collect(
            ArrayList::new,
            (list, value) -> list.add(new SensorData(value)),
            ArrayList::addAll
        );

        System.out.println("Collected Sensor Data: " + sensorDataList);
    }
}

Output:

Collected Sensor Data: [SensorData{value=25.3}, SensorData{value=26.7}, SensorData{value=23.8}, SensorData{value=24.1}, SensorData{value=25.9}]

Conclusion

The DoubleStream.collect() method is used to perform a mutable reduction operation on the elements of a stream. This method is highly versatile and can be used to transform the elements into different forms, such as collections or custom data structures. By understanding and using this method, you can efficiently manage and manipulate streams of double values in your Java applications.

Comments