What is the difference between using Collections.sort() and Stream.sorted() in Java 8+?

Collections.sort() and Stream.sorted() are both used for sorting data in Java, but they differ in how they operate and what they return. Collections.sort() performs an in-place sort by modifying the original list, whereas Stream.sorted() follows a functional programming approach by producing a new sorted stream without changing the source collection.

Key Points: • Collections.sort() updates the original list directly, while Stream.sorted() leaves the original collection unchanged. • Stream.sorted() supports method chaining and works seamlessly with other stream operations such as filter(), map(), and collect(). • Collections.sort() is suitable when you want to sort an existing list, whereas Stream.sorted() is ideal for immutable and functional-style data processing.

Example: Suppose you have a list of employee names. If you use Collections.sort(), the original list becomes sorted. If you use Stream.sorted(), the original list remains unchanged, and a new sorted result can be collected into another list.

Code Example:

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class SortExample {

    public static void main(String[] args) {

        List<String> names =
                Arrays.asList("John", "David", "Alice");

        Collections.sort(names);
        System.out.println("Collections.sort(): " + names);

        List<String> sortedNames =
                names.stream()

.sorted()

                     .collect(Collectors.toList());

        System.out.println("Stream.sorted(): " + sortedNames);
    }
}

Interview Tip: A concise interview answer is: Collections.sort() sorts and modifies the original list, whereas Stream.sorted() returns a new sorted stream without changing the source collection. Stream.sorted() is preferred in Java 8+ when using the Stream API and functional programming style.