Java DoubleStream distinct() Method

The distinct() method in Java, part of the java.util.stream.DoubleStream interface, is used to return a stream consisting of the distinct elements of the stream. This method is useful when you need to eliminate duplicate elements from a stream of double values.

Table of Contents

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

Introduction

The distinct() method returns a new stream consisting of the distinct elements of the original stream. It ensures that duplicate elements are removed from the stream, leaving only unique elements.

distinct() Method Syntax

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

DoubleStream distinct()

Parameters:

  • This method does not take any parameters.

Returns:

  • A DoubleStream consisting of the distinct elements of the original stream.

Throws:

  • This method does not throw any exceptions.

Understanding distinct()

The distinct() method allows you to eliminate duplicate elements from a DoubleStream. The resulting stream contains only unique elements, preserving the order of the first occurrence of each element.

Examples

Basic Usage

To demonstrate the basic usage of distinct(), we will create a DoubleStream with duplicate elements and use distinct() to remove duplicates.

Example

import java.util.stream.DoubleStream;

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

        // Get distinct elements of the stream
        DoubleStream distinctStream = doubleStream.distinct();

        // Print the distinct stream
        distinctStream.forEach(System.out::println);
    }
}

Output:

1.1
2.2
3.3
4.4
5.5

Using distinct() with a Sorted Stream

This example shows how to use distinct() with a sorted stream to remove duplicates and then sort the unique elements.

Example

import java.util.stream.DoubleStream;

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

        // Get distinct elements and sort them
        DoubleStream distinctSortedStream = doubleStream.distinct().sorted();

        // Print the distinct sorted stream
        distinctSortedStream.forEach(System.out::println);
    }
}

Output:

1.1
2.2
3.3
4.4
5.5

Real-World Use Case

Removing Duplicate Sensor Readings

In real-world applications, the distinct() method can be used to remove duplicate sensor readings from a stream of sensor data.

Example

import java.util.stream.DoubleStream;

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

        // Get distinct sensor readings
        DoubleStream distinctSensorData = sensorData.distinct();

        // Print the distinct sensor readings
        distinctSensorData.forEach(System.out::println);
    }
}

Output:

25.3
26.7
24.8
27.3

Conclusion

The DoubleStream.distinct() method is used to remove duplicate elements from a stream. This method is particularly useful for ensuring that the stream contains only unique elements. By understanding and using this method, you can efficiently manage and manipulate streams of double values in your Java applications.

Comments