🎓 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 sorted() method in Java, part of the java.util.stream.DoubleStream interface, is used to return a stream consisting of the elements of the original stream, sorted in ascending order. This method is useful when you need to sort the elements of a stream before performing further operations.
Table of Contents
- Introduction
sorted()Method Syntax- Understanding
sorted() - Examples
- Basic Usage
- Using
sorted()with Other Stream Operations
- Real-World Use Case
- Conclusion
Introduction
The sorted() method returns a new DoubleStream consisting of the elements of the original stream, sorted in ascending order. This method is an intermediate operation, meaning it returns a new stream and does not modify the original stream.
sorted() Method Syntax
The syntax for the sorted() method is as follows:
DoubleStream sorted()
Parameters:
- This method does not take any parameters.
Returns:
- A new
DoubleStreamconsisting of the elements of the original stream, sorted in ascending order.
Throws:
- This method does not throw any exceptions.
Understanding sorted()
The sorted() method allows you to sort the elements of a DoubleStream in ascending order. This can be particularly useful when you need the elements to be in a specific order for further processing or analysis.
Examples
Basic Usage
To demonstrate the basic usage of sorted(), we will create a DoubleStream and use sorted() to sort its elements in ascending order.
Example
import java.util.stream.DoubleStream;
public class SortedExample {
public static void main(String[] args) {
DoubleStream doubleStream = DoubleStream.of(5.5, 3.3, 1.1, 4.4, 2.2);
// Sort the elements of the stream
DoubleStream sortedStream = doubleStream.sorted();
// Print the sorted elements
sortedStream.forEach(System.out::println);
}
}
Output:
1.1
2.2
3.3
4.4
5.5
Using sorted() with Other Stream Operations
This example shows how to use sorted() in combination with other stream operations, such as filtering and mapping.
Example
import java.util.stream.DoubleStream;
public class SortedWithOtherOperationsExample {
public static void main(String[] args) {
DoubleStream doubleStream = DoubleStream.of(5.0, 3.0, 1.0, 4.0, 2.0);
// Filter, sort, and map the elements of the stream
DoubleStream processedStream = doubleStream.filter(n -> n > 2.0)
.sorted()
.map(n -> n * 2);
// Print the processed elements
processedStream.forEach(System.out::println);
}
}
Output:
6.0
8.0
10.0
Real-World Use Case
Sorting Temperature Readings
In real-world applications, the sorted() method can be used to sort temperature readings before performing further analysis.
Example
import java.util.stream.DoubleStream;
public class TemperatureReadingsSortedExample {
public static void main(String[] args) {
DoubleStream temperatureReadings = DoubleStream.of(25.3, 26.7, 24.8, 27.5, 30.1);
// Sort the temperature readings
DoubleStream sortedReadings = temperatureReadings.sorted();
// Print the sorted temperature readings
sortedReadings.forEach(reading -> System.out.println("Sorted reading: " + reading));
}
}
Output:
Sorted reading: 24.8
Sorted reading: 25.3
Sorted reading: 26.7
Sorted reading: 27.5
Sorted reading: 30.1
Conclusion
The DoubleStream.sorted() method is used to sort the elements of the stream in ascending order. This method is particularly useful for scenarios where you need the elements to be in a specific order for further processing or analysis. 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