How to Sort List Using Streams API

Introduction

Sorting a list of objects is a very common problem faced in Java. In this article, we will learn how to sort list traditionally and then optimize it using Java 8 streams API.

Traditional Approach

For sorting a list using the traditional approach, we can simply use Collections.sort() methods

Code Example:-


public class SortListWithoutStreams {

    public static void main(String[] args) {

        List<Integer> list=new ArrayList<>();
        list.add(8);
        list.add(3);
        list.add(12);
        list.add(4);

        Collections.sort(list); //ASCENDING
        System.out.println(list); // o/p:- [3, 4, 8, 12]

        Collections.reverse(list); //DESCENDING
        System.out.println(list);  // [12, 8, 4, 3]

    }
}

Streams Approach

For sorting a list using the streams approach, we can make use of the Comparator functional interface inside the sorted method for Java 8 streams.

Code Example:

public class SortListWithStreams {
public static void main(String[] args) {

        List<Integer> list=new ArrayList<>();
        list.add(8);
        list.add(3);
        list.add(12);
        list.add(4);

        list.stream().sorted().forEach(s->System.out.println(s));
        //ASCENDING
        // o/p:- [3, 4, 8, 12]

        list.stream().sorted(Comparator.reverseOrder()).
        forEach(s->System.out.println(s)); //DESCENDING
        // o/p:- [12, 8, 4, 3]

    }
}

Real-world Example

Suppose there is a list of Employees and we want to apply custom sorting on that list, then Java 8 streams provide the most optimized and readable solution to sort it using the Comparator Functional Interface.

Code Example:-

Employee.java

public class Employee {

    private int id;
    private String name;
    private String dept;
    private long salary;

    //CONSTRUCTORS, GETTERS,SETTERS AND TOSTRING
    }

Database.java

public class DataBase {

    public static List<Employee> getEmployees(){
        List<Employee> list=new ArrayList<>();
        list.add(new Employee(176,"Roshan","IT",600000));
        list.add(new Employee(388,"Bikash","Civil",900000));
        list.add(new Employee(246,"Bikash","DEFENCE",700000));
        list.add(new Employee(568,"Roshan","CORE",800000));
        return list;
    }
}

SortEmployeeList.java

public class SortEmployee {

    public static void main(String[] args) {

        List<Employee> employees=DataBase.getEmployees();

        //sort in ascending order of salary
        List<Employee> sortedEmployeeListSalary=employees.stream()
                .sorted(Comparator.comparing(Employee::getSalary))
                .collect(Collectors.toList());

        System.out.println(sortedEmployeeListSalary);
        // o/p :- [Employee [id=176, name=Roshan, dept=IT, salary=600000],
        //Employee [id=246, name=Bikash, dept=DEFENCE, salary=700000],
        //Employee [id=568, name=Roshan, dept=CORE, salary=800000], 
        //Employee [id=388, name=Bikash, dept=Civil, salary=900000]]


        //sort in desc order of name and then asc order of salary
        List<Employee> sortedEmployeeListNameDescSalary=employees.stream().
                sorted(Comparator.comparing(Employee::getName).reversed()
                        .thenComparing(Employee::getSalary))
                .collect(Collectors.toList());

        System.out.println(sortedEmployeeListNameDescSalary);
        //o/p :- [Employee [id=176, name=Roshan, dept=IT, salary=600000], 
        //Employee [id=568, name=Roshan, dept=CORE, salary=800000], 
        //Employee [id=246, name=Bikash, dept=DEFENCE,  salary=700000], 
        //Employee [id=388, name=Bikash, dept=Civil, salary=900000]]


    }

}

Summary

In this post, we have learned about how to sort Lists using Java 8 Streams API.

Thank you for reading. If you found this article helpful, react and share. Cheers.