1

I have been trying different ways of sorting and 1 of the methods I tried does not give the output I would expect.

I get the expected output from the Lamda sort that is commented out in the code

employees.sort(Comparator.comparing(Employee::getName).thenComparing(Employee::getAge));
Jack 40
John 29
John 30
Snow 22
Tim 21

but from

employees.stream().sorted(Comparator.comparing(Employee::getName).thenComparing(Employee::getAge));

I get this incorrect output

John 30
John 29
Tim 21
Jack 40
Snow 22

Where have I made the mistake?

package com;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class Main {

    public static void main(String[] args) {
        Employee john = new Employee("John", 30);
        Employee john2 = new Employee("John", 29);
        Employee tim = new Employee("Tim", 21);
        Employee jack = new Employee("Jack", 40);
        Employee snow = new Employee("Snow", 22);

        List<Employee> employees = new ArrayList<>();
        employees.add(john);
        employees.add(john2);
        employees.add(tim);
        employees.add(jack);
        employees.add(snow);

        employees.stream().sorted(Comparator.comparing(Employee::getName).thenComparing(Employee::getAge)); //does not sort original list

        //employees.sort(Comparator.comparing(Employee::getName).thenComparing(Employee::getAge)); //sorts original list

        for(Employee employee : employees){
            System.out.println(employee.getName() + " " + employee.getAge());
        }
    }
}

class Employee{
    private String name;
    private int age;

    public Employee(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

2
  • 1
    The question should be around the stream and not lambda. The first thing you would find in the doc of streams is the type of operations and their importance. Commented Aug 20, 2020 at 17:33
  • 1
    There is nothing wrong with your code, you just need to look further into the Stream API. You successfully sort the stream but you don't reassign it to your List. When you call stream() on a collection a Stream object gets created which is not referencing to your initial collection. Commented Aug 20, 2020 at 17:36

3 Answers 3

2

.sorted() is not a terminal operation. In fact, nothing happens in below line of code as Streams are lazy. To confirm, just add .peek(System.out::println) and you'll see that this is never executed. To sort, add terminal operation .collect(Collectors.toList())

employees.stream().sorted(Comparator.comparing(Employee::getName)
                                    .thenComparing(Employee::getAge));

From documentation -

Streams are lazy; computation on the source data is only performed when the terminal operation is initiated, and source elements are consumed only as needed.

Sign up to request clarification or add additional context in comments.

Comments

1

The working example sorts the original list but with stream it returns a new sorted one. Example -You have to collect it like below , and employeeList is sorted as other result.

final List<Employee> employeeList = employees.stream().sorted(Comparator.comparing(Employee::getName).thenComparing(Employee::getAge)).collect(Collectors.toList());

Comments

1

Stream doesn't modify existing collection. You have to collect(using terminal operation) it to see changes you made on stream.

List<Employee> sortedEmployees = employees.stream().sorted(Comparator.comparing(Employee::getName).thenComparing(Employee::getAge)).collect(Collectors.toList())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.