🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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
- Introduction
distinct()Method Syntax- Understanding
distinct() - Examples
- Basic Usage
- Using
distinct()with a Sorted Stream
- Real-World Use Case
- 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
DoubleStreamconsisting 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.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment