How to sort employee object in java 8 using stream?

Sort Employee Object using java 8 stream API
Www.Koverstory.com

Today we will try to write a java program to sort Employee object in Ascending and Descending order using java 8 stream.

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

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

        List<Employee> empList = new ArrayList<Employee>();
        empList.add(new Employee(100,"RAM","ram@gmail.com"));
        empList.add(new Employee(90,"SHYAM","shyam@gmail.com"));
        empList.add(new Employee(50,"HANUMAAN","hanumaan@gmail.com"));
        empList.add(new Employee(40,"GHANSHYAM","ghanshyam@gmail.com"));
        empList.add(new Employee(30,"RADHA","radha@gmail.com"));
        empList.add(new Employee(10,"MURLI","murli@gmail.com"));

        System.out.println("================Employee Object BEFORE Sorting=======================");

        System.out.println(empList);

        System.out.println("================Employee Object AFTER Sorting in ASCENDING ORDER=======================");

        List<Employee> sortedEmployeeAsc = empList.stream().sorted(Comparator.comparingInt(Employee::getId)).collect(Collectors.toList());

        System.out.println(sortedEmployeeAsc);

        System.out.println("================Employee Object AFTER Sorting in DESCENDING ORDER=======================");

        List<Employee> sortedEmployeeDesc = empList.stream().sorted(Comparator.comparingInt(Employee::getId).reversed()).collect(Collectors.toList());

        System.out.println(sortedEmployeeDesc);

    }
}

class Employee{

    int id;
    String name;
    String email;

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

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", email='" + email + '\'' +
                '}';
    }
}

OUTPUT

================Employee Object BEFORE Sorting=======================
[Employee{id=100, name=’RAM’, email=’ram@gmail.com’}, Employee{id=90, name=’SHYAM’, email=’shyam@gmail.com’}, Employee{id=50, name=’HANUMAAN’, email=’hanumaan@gmail.com’}, Employee{id=40, name=’GHANSHYAM’, email=’ghanshyam@gmail.com’}, Employee{id=30, name=’RADHA’, email=’radha@gmail.com’}, Employee{id=10, name=’MURLI’, email=’murli@gmail.com’}]

================Employee Object AFTER Sorting in ASCENDING ORDER=======================
[Employee{id=10, name=’MURLI’, email=’murli@gmail.com’}, Employee{id=30, name=’RADHA’, email=’radha@gmail.com’}, Employee{id=40, name=’GHANSHYAM’, email=’ghanshyam@gmail.com’}, Employee{id=50, name=’HANUMAAN’, email=’hanumaan@gmail.com’}, Employee{id=90, name=’SHYAM’, email=’shyam@gmail.com’}, Employee{id=100, name=’RAM’, email=’ram@gmail.com’}]

================Employee Object AFTER Sorting in DESCENDING ORDER=======================
[Employee{id=100, name=’RAM’, email=’ram@gmail.com’}, Employee{id=90, name=’SHYAM’, email=’shyam@gmail.com’}, Employee{id=50, name=’HANUMAAN’, email=’hanumaan@gmail.com’}, Employee{id=40, name=’GHANSHYAM’, email=’ghanshyam@gmail.com’}, Employee{id=30, name=’RADHA’, email=’radha@gmail.com’}, Employee{id=10, name=’MURLI’, email=’murli@gmail.com’}]

how to setup latest version of  java 20

One thought on “How to sort employee object in java 8 using stream?

Leave a Reply

Your email address will not be published. Required fields are marked *