SoFunction
Updated on 2025-04-22

Summary of six core techniques for implementing List sorting in Stream in Java

1. Basic sorting implementation

1.1 Natural order sorting (positive order)

List<Entity> sortedList = ()
        .sorted((Entity::getId))
        .collect(());

1.2 Reverse sorting (reverse order)

List<Entity> sortedList = ()
        .sorted((Entity::getId).reversed())
        .collect(());

2. Advanced sorting skills

2.1 Null value safety processing

// Handle fields that may be nullComparator&lt;Entity&gt; nullSafeComparator = (
    Entity::getId, 
    (())
);

List&lt;Entity&gt; sortedList = ()
        .sorted(nullSafeComparator)
        .collect(());

2.2 Multi-field combination sorting

List<Entity> sortedList = ()
        .sorted((Entity::getDepartment)
                .thenComparing(Entity::getId))
        .collect(());

3. Performance optimization suggestions

3.1 Parallel flow acceleration (suitable for large data volume)

List<Entity> sortedList = ()
        .sorted((Entity::getId))
        .collect(());

3.2 Order in place (modify the original collection)

((Entity::getId));

4. Best Practices

  • Type definition: It is recommended to specify the specific collection type
ArrayList<Entity> sortedList = ()
        .sorted((Entity::getId))
        .collect((ArrayList::new));
  • Defensive copy: Keep the original collection immutable
List<Entity> sortedList = new ArrayList<>(originalList);
((Entity::getId));
  • Lambda Optimization: Use Lambda expressions in complex scenarios
List&lt;Entity&gt; sortedList = ()
        .sorted((e1, e2) -&gt; {
            // Custom comparison logic            return ().compareTo(());
        })
        .collect(());

5. Things to note

  • Immutability()The returned List implementation may not support modification
  • Null pointer protection: Recommended to use/nullsLast
  • Performance trade-offs: When more than 100,000 data are preferred to traditional sorting methods
  • Object Status: Stream operation will not modify the original collection element

6. Complete example

public class SortingDemo {
    public static void main(String[] args) {
        List&lt;Entity&gt; entities = (
            new Entity(2, "B"),
            new Entity(1, "A"),
            new Entity(3, "C")
        );

        // Multi-condition sorting: first reverse order by name, then positive order by ID        List&lt;Entity&gt; sorted = ()
                .sorted((Entity::getName)
                        .reversed()
                        .thenComparing(Entity::getId))
                .collect(());

        (::println);
    }
}

class Entity {
    private int id;
    private String name;
    
    // Constructor method and getter omitted}

7. Summary and comparison

sort by Time complexity Space complexity Applicable scenarios
Stream sequential streams O(n log n) O(n) General Scenario
Stream Parallel Stream O(n log n) O(n) Big data volume (10w+)
O(n log n) O(1) Modify requirements in place
Database sorting O(n log n) O(1) When the data source is in the database

By rationally selecting the sorting strategy, you can ensure the simplicity of the code while taking into account system performance. It is recommended to choose the most appropriate sorting method based on the actual business scenario.

This is the end of this article about the six core techniques for implementing List sorting in Stream in Java. For more related content on Java Stream implementation List sorting, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!