Java Stream sorted() Method with Examples

Ramesh Fadatare
3 min readSep 26, 2024

--

The sorted() method in Java is a part of the java.util.stream.Stream interface. In this guide, we will learn how to use sorted() method in Java with practical examples and real-world use cases to better understand its functionality.

Learn Java from scratch: Learn Java Programming.

Complete Java Reference: Java API Documentation.

Table of Contents

  1. Introduction
  2. sorted() Method Syntax
  3. Understanding sorted()
  4. Examples
  5. Real-World Use Case
  6. Conclusion

Introduction

The sorted() method is an intermediate operation that returns a stream consisting of the elements of the original stream, sorted either according to their natural order or by a specified Comparator. This method does not modify the original stream but produces a new sorted stream.

sorted() Method Syntax

1. Using Natural Order

Stream<T> sorted()

2. Using a Custom Comparator

Stream<T> sorted(Comparator<? super T> comparator)

Parameters:

  • comparator (optional): A Comparator to compare elements of the stream.

Returns:

  • A new Stream consisting of the elements of the original stream, sorted according to the specified order.

Throws:

  • This method does not throw any exceptions.

Understanding sorted()

The sorted() method allows you to sort the elements of a stream. When no comparator is provided, the elements are sorted according to their natural order. When a comparator is provided, the elements are sorted according to the order defined by the comparator.

Examples

Basic Usage (Natural Order)

To demonstrate the basic usage of sorted() with natural order, we will create a Stream of integers and sort them.

import java.util.stream.Stream;

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

// Use sorted() to sort the elements in natural order
Stream<Integer> sortedStream = stream.sorted();

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

Output:

1
2
3
4
5

Using sorted() with a Custom Comparator

This example shows how to use sorted() with a custom comparator to sort strings by their length.

import java.util.Comparator;
import java.util.stream.Stream;

public class SortedWithComparatorExample {
public static void main(String[] args) {
Stream<String> stream = Stream.of("apple", "banana", "cherry", "date");

// Use sorted() with a custom comparator to sort the elements by length
Stream<String> sortedStream = stream.sorted(Comparator.comparingInt(String::length));

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

Output:

date
apple
banana
cherry

Real-World Use Case: Sorting Employees by Salary

In real-world applications, the sorted() method can be used to sort complex objects, such as sorting a list of employees by their salary.

Example

import java.util.Comparator;
import java.util.stream.Stream;

public class SortedRealWorldExample {
static class Employee {
String name;
double salary;

Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}

double getSalary() {
return salary;
}

@Override
public String toString() {
return name + ": " + salary;
}
}

public static void main(String[] args) {
Stream<Employee> employees = Stream.of(
new Employee("Alice", 50000),
new Employee("Bob", 60000),
new Employee("Charlie", 40000)
);

// Use sorted() with a custom comparator to sort employees by salary
Stream<Employee> sortedEmployees = employees.sorted(Comparator.comparingDouble(Employee::getSalary));

// Print the sorted employees
sortedEmployees.forEach(System.out::println);
}
}

Output:

Charlie: 40000.0
Alice: 50000.0
Bob: 60000.0

Conclusion

The Stream.sorted() method is used to sort the elements of a stream either according to their natural order or by a specified Comparator. This method is particularly useful for ordering elements in a specific sequence.

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.

Original Post:

https://www.rameshfadatare.com/java-stream/java-stream-sorted-method/

--

--

No responses yet