Java IntStream sorted() Method

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

  1. Introduction
  2. sorted() Method Syntax
  3. Understanding sorted()
  4. Examples
    • Basic Usage
    • Using sorted() with Other Stream Operations
  5. Real-World Use Case
  6. 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 IntStream consisting 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.

Comments