Java LongStream sorted() Method

The sorted() method in Java, part of the java.util.stream.LongStream interface, is used to return a stream consisting of the elements of the original stream, sorted in natural order. This method is useful when you need to sort the elements of a stream before performing further operations on them.

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 new stream consisting of the elements of the original stream, sorted in ascending order according to their natural ordering. 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:

LongStream sorted()

Parameters:

  • This method does not take any parameters.

Returns:

  • A new LongStream consisting of the elements of the original stream, sorted in natural order.

Throws:

  • This method does not throw any exceptions.

Understanding sorted()

The sorted() method sorts the elements of the stream in natural order. It is an intermediate operation, meaning that it creates a new stream and does not alter the original stream. Sorting can be useful in many contexts, such as preparing data for aggregation or analysis.

Examples

Basic Usage

To demonstrate the basic usage of sorted(), we will create a LongStream and use sorted() to sort the elements.

Example

import java.util.stream.LongStream;

public class SortedExample {
    public static void main(String[] args) {
        LongStream stream = LongStream.of(5L, 3L, 1L, 4L, 2L);

        // Use sorted() to sort the elements
        LongStream sortedStream = stream.sorted();

        // Print the elements of the sorted stream
        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 filtering and mapping.

Example

import java.util.stream.LongStream;

public class SortedWithFilterAndMapExample {
    public static void main(String[] args) {
        LongStream stream = LongStream.of(5L, 3L, 1L, 4L, 2L);

        // Use sorted() to sort the elements after filtering and mapping
        LongStream sortedStream = stream
            .filter(n -> n % 2 == 0) // Keep only even numbers
            .map(n -> n * 10)        // Multiply by 10
            .sorted();               // Sort the elements

        // Print the elements of the sorted stream
        sortedStream.forEach(System.out::println);
    }
}

Output:

20
40

Real-World Use Case

Sorting Transaction Amounts

In real-world applications, the sorted() method can be used to sort transaction amounts in ascending order before performing further analysis or processing.

Example

import java.util.stream.LongStream;

public class SortedTransactionsExample {
    public static void main(String[] args) {
        LongStream transactionAmounts = LongStream.of(2500L, 1000L, 3000L, 2000L, 1500L);

        // Use sorted() to sort the transaction amounts
        LongStream sortedTransactions = transactionAmounts.sorted();

        // Print the sorted transaction amounts
        sortedTransactions.forEach(amount -> System.out.println("Transaction Amount: " + amount));
    }
}

Output:

Transaction Amount: 1000
Transaction Amount: 1500
Transaction Amount: 2000
Transaction Amount: 2500
Transaction Amount: 3000

Conclusion

The LongStream.sorted() method is used to return a stream consisting of the elements of the original stream, sorted in natural order. This method is particularly useful for sorting elements before performing further operations on them. By understanding and using this method, you can efficiently manage and process streams of values in your Java applications, ensuring that elements are sorted as needed.

Comments