Learn Java 8 streams with an example - How to sort a list

 Example 1: Sort the List In the natural order

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

public class DriverClass {
public static void main(String[] args) {
List<String> list = Arrays.asList("H", "A", "Z", "L", "B", "Y", "M", "a", "c");
list.stream().
sorted().collect(Collectors.toList()).forEach
                                    (System.out::println);
}
}
Output:

A
B
H
L
M
Y
Z
a
c



Example 2: Sort the List In the reverse Order

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

public class DriverClass {

public static void main(String[] args) {
List<String> list = Arrays.asList("H", "A", "Z", "L", "B", "Y", "M", "a", "c");
list.stream().
sorted(Comparator.reverseOrder()).
                  collect(Collectors.toList()).forEach(System.out::println);
}
}
Output:

c
a
Z
Y
M
L
H
B
A



Example 3: Sort the List Objects In the natural order (sort by salary)

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

public class DriverClass {
static List<Employee> employees = Arrays.asList(
new Employee("Alpha", 3000),
new Employee("Beta", 40000),
new Employee("Noob", 10000),
new Employee("Pro", 200000),
new Employee("Sibin", 5000));

public static void main(String[] args) {
List<Employee> sortedList = employees.stream()
.sorted(Comparator.comparingInt(Employee::getSalary))
.collect(Collectors.
toList());
sortedList.forEach(System.
out::println);
}

static class Employee {
private String name;
private int salary;

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

public String getName() {
return name;
}

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

public int getSalary() {
return salary;
}

public void setSalary(int salary) {
this.salary = salary;
}

@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", salary=" + salary +
'}';
}
}
}
Output:

Employee{name='Alpha', salary=3000}
Employee{name='Sibin', salary=5000}
Employee{name='Noob', salary=10000}
Employee{name='Beta', salary=40000}
Employee{name='Pro', salary=200000}


Example 4: Sort the List Objects In the natural order (sort by name)

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

public class DriverClass {
static List<Employee> employees = Arrays.asList(
new Employee("Beta", 3000),
new Employee("Alpha", 40000),
new Employee("Pro", 10000),
new Employee("Noob", 200000),
new Employee("Sibin", 5000));

public static void main(String[] args) {
List<Employee> sortedList = employees.stream()
.sorted(Comparator.comparing(Employee::getName))
.collect(Collectors.
toList());
sortedList.forEach(System.
out::println);
}

static class Employee {
private String name;
private int salary;

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

public String getName() {
return name;
}

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

public int getSalary() {
return salary;
}

public void setSalary(int salary) {
this.salary = salary;
}

@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", salary=" + salary +
'}';
}
}
}
Output:

Employee{name='Alpha', salary=40000}
Employee{name='Beta', salary=3000}
Employee{name='Noob', salary=200000}
Employee{name='Pro', salary=10000}
Employee{name='Sibin', salary=5000}


Example 5: Sort the List Objects In the reverse order (sort by salary)

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

public class DriverClass {
static List<Employee> employees = Arrays.asList(
new Employee("Beta", 3000),
new Employee("Alpha", 40000),
new Employee("Pro", 10000),
new Employee("Noob", 200000),
new Employee("Sibin", 5000));

public static void main(String[] args) {
List<Employee> sortedList = employees.stream()
.sorted(Comparator.comparingInt(Employee::getSalary)
.reversed())

.collect(Collectors.
toList());
sortedList.forEach(System.
out::println);
}

static class Employee {
private String name;
private int salary;

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

public String getName() {
return name;
}

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

public int getSalary() {
return salary;
}

public void setSalary(int salary) {
this.salary = salary;
}

@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", salary=" + salary +
'}';
}
}
}
Output:

Employee{name='Noob', salary=200000}
Employee{name='Alpha', salary=40000}
Employee{name='Pro', salary=10000}
Employee{name='Sibin', salary=5000}
Employee{name='Beta', salary=3000}


Example 6: Sort the List Objects In the reverse order (sort by name)

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

public class DriverClass {
static List<Employee> employees = Arrays.asList(
new Employee("Beta", 3000),
new Employee("Alpha", 40000),
new Employee("Pro", 10000),
new Employee("Noob", 200000),
new Employee("Sibin", 5000));

public static void main(String[] args) {
List<Employee> sortedList = employees.stream()
.sorted(Comparator.comparing(Employee::getName)
.reversed())
.collect(Collectors.toList());
sortedList.forEach(System.out::println);
}

static class Employee {
private String name;
private int salary;

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

public String getName() {
return name;
}

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

public int getSalary() {
return salary;
}

public void setSalary(int salary) {
this.salary = salary;
}

@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", salary=" + salary +
'}';
}
}
}
Output:

Employee{name='Sibin', salary=5000}
Employee{name='Pro', salary=10000}
Employee{name='Noob', salary=200000}
Employee{name='Beta', salary=3000}
Employee{name='Alpha', salary=40000}


More...


Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete