🎓 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.IntStream 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 process elements in a specific order.
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 stream 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:
IntStream sorted()
Parameters:
- This method does not take any parameters.
Returns:
- A new
IntStreamconsisting of the elements of the original stream, sorted in ascending order.
Throws:
- This method does not throw any exceptions.
Understanding sorted()
The sorted() method processes the elements of the stream and returns a new stream with the elements sorted in ascending order. This is particularly useful for scenarios where the order of elements matters.
Examples
Basic Usage
To demonstrate the basic usage of sorted(), we will create an IntStream with unsorted elements and use sorted() to sort them.
Example
import java.util.stream.IntStream;
public class SortedExample {
public static void main(String[] args) {
IntStream intStream = IntStream.of(5, 3, 1, 4, 2);
// Use sorted() to sort the elements of the stream
IntStream sortedStream = intStream.sorted();
// Print the sorted elements
sortedStream.forEach(System.out::println);
}
}
Output:
1
2
3
4
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.IntStream;
public class SortedWithOtherOperationsExample {
public static void main(String[] args) {
IntStream intStream = IntStream.of(9, 7, 5, 8, 6);
// Filter even numbers, map them to their squares, and sort
IntStream processedStream = intStream.filter(n -> n % 2 == 0)
.map(n -> n * n)
.sorted();
// Print the processed elements
processedStream.forEach(System.out::println);
}
}
Output:
36
64
Real-World Use Case
Sorting Scores
In real-world applications, the sorted() method can be used to sort scores or other numerical data in ascending order.
Example
import java.util.stream.IntStream;
public class SortScoresExample {
public static void main(String[] args) {
IntStream scores = IntStream.of(85, 92, 75, 88, 95);
// Use sorted() to sort the scores
IntStream sortedScores = scores.sorted();
// Print the sorted scores
sortedScores.forEach(score -> System.out.println("Score: " + score));
}
}
Output:
Score: 75
Score: 85
Score: 88
Score: 92
Score: 95
Conclusion
The IntStream.sorted() method is used to return a stream consisting of the elements of the original stream, sorted in ascending order. This method is particularly useful for scenarios where the order of elements matters. By understanding and using this method, you can efficiently manage and process streams of integer values in your Java applications, ensuring elements are processed in the desired order.
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