Java DoubleStream peek() Method

The peek() method in Java, part of the java.util.stream.DoubleStream interface, is used to perform an action for each element of the stream as it is consumed. This method is useful for debugging purposes or for performing operations that do not modify the stream's elements.

Table of Contents

  1. Introduction
  2. peek() Method Syntax
  3. Understanding peek()
  4. Examples
    • Basic Usage
    • Using peek() for Debugging
  5. Real-World Use Case
  6. Conclusion

Introduction

The peek() method returns a new DoubleStream consisting of the elements of the original stream, with an additional action performed on each element as it is consumed. This method is an intermediate operation, meaning it returns a new stream and does not modify the original stream.

peek() Method Syntax

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

DoubleStream peek(DoubleConsumer action)

Parameters:

  • action: A DoubleConsumer that represents the action to be performed on each element of the stream.

Returns:

  • A new DoubleStream consisting of the elements of the original stream, with the specified action applied to each element.

Throws:

  • This method does not throw any exceptions.

Understanding peek()

The peek() method is often used for debugging purposes or for performing operations that do not modify the elements of the stream. It allows you to observe the elements of the stream as they pass through the pipeline, making it easier to understand the flow of data and identify any issues.

Examples

Basic Usage

To demonstrate the basic usage of peek(), we will create a DoubleStream and use peek() to print each element as it is consumed.

Example

import java.util.stream.DoubleStream;

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

        // Use peek to print each element as it is consumed
        doubleStream.peek(System.out::println)
                    .map(n -> n * 2)
                    .forEach(System.out::println);
    }
}

Output:

1.1
2.2
2.2
4.4
3.3
6.6
4.4
8.8
5.5
11.0

Using peek() for Debugging

This example shows how to use peek() to debug a stream pipeline by printing the elements at different stages of processing.

Example

import java.util.stream.DoubleStream;

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

        // Use peek to print the elements at different stages of processing
        doubleStream.peek(n -> System.out.println("Original: " + n))
                    .map(n -> n * n)
                    .peek(n -> System.out.println("Squared: " + n))
                    .filter(n -> n > 10)
                    .peek(n -> System.out.println("Filtered: " + n))
                    .forEach(System.out::println);
    }
}

Output:

Original: 1.0
Squared: 1.0
Original: 2.0
Squared: 4.0
Original: 3.0
Squared: 9.0
Original: 4.0
Squared: 16.0
Filtered: 16.0
16.0
Original: 5.0
Squared: 25.0
Filtered: 25.0
25.0

Real-World Use Case

Logging Sensor Data

In real-world applications, the peek() method can be used to log sensor data as it is processed through a stream pipeline.

Example

import java.util.stream.DoubleStream;

public class SensorDataLoggingExample {
    public static void main(String[] args) {
        DoubleStream sensorData = DoubleStream.of(25.3, 26.7, 24.8, 27.5, 30.1);

        // Use peek to log the sensor data at different stages of processing
        sensorData.peek(reading -> System.out.println("Original reading: " + reading))
                  .map(reading -> reading * 1.05) // Apply a calibration factor
                  .peek(reading -> System.out.println("Calibrated reading: " + reading))
                  .filter(reading -> reading > 25.0) // Filter readings above 25.0
                  .peek(reading -> System.out.println("Filtered reading: " + reading))
                  .forEach(System.out::println);
    }
}

Output:

Original reading: 25.3
Calibrated reading: 26.565
Filtered reading: 26.565
26.565
Original reading: 26.7
Calibrated reading: 28.035
Filtered reading: 28.035
28.035
Original reading: 24.8
Calibrated reading: 26.040000000000003
Filtered reading: 26.040000000000003
26.040000000000003
Original reading: 27.5
Calibrated reading: 28.875
Filtered reading: 28.875
28.875
Original reading: 30.1
Calibrated reading: 31.605000000000004
Filtered reading: 31.605000000000004
31.605000000000004

Conclusion

The DoubleStream.peek() method is used to perform an action for each element of the stream as it is consumed. This method is particularly useful for debugging purposes or for performing operations that do not modify the elements of the stream. By understanding and using this method, you can efficiently observe and log the elements of a stream in your Java applications.

Comments