Java 8 Stream – How to sort a List with stream.sorted()
In this section, we will show you how to sort list using stream.sorted() in Java 8.
1. List
1.1 Sort a List in natural order.
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<String> list = Arrays.
asList("8", "B", "A", "1", "Z", "Y", "2", "b", "d");
List<String> sortedList1 = list.stream()
.sorted(Comparator.naturalOrder())
.collect(Collectors.toList());
System.out.println(sortedList1);
List<String> sortedList2 = list.stream()
.sorted((o1,o2)-> o1.compareTo(o2))
.collect(Collectors.toList());
System.out.println(sortedList2);
List<String> sortedList3 = list.stream().
sorted().collect(Collectors.toList());
System.out.println(sortedList3);
}
}
Console Output:
[1, 2, 8, A, B, Y, Z, b, d]
[1, 2, 8, A, B, Y, Z, b, d]
[1, 2, 8, A, B, Y, Z, b, d]
1.2 Sort a List in reverse order.
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<String> list = Arrays.
asList("8", "B", "A", "1", "Z", "Y", "2", "b", "d");
List<String> sortedList1 = list.stream()
.sorted(Comparator.reverseOrder())
.collect(Collectors.toList());
System.out.println(sortedList1);
List<String> sortedList2 = list.stream()
.sorted((o1,o2)-> o2.compareTo(o1))
.collect(Collectors.toList());
System.out.println(sortedList2);
}
}
Console Output:
[d, b, Z, Y, B, A, 8, 2, 1]
[d, b, Z, Y, B, A, 8, 2, 1]
2. List Objects
1.1 Sort by salary, natural order.
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class Main {
static List<Employee> employees = Arrays.asList(
new Employee("Alpha", 3000.45d),
new Employee("Beta", 2500.50d),
new Employee("Gama", 1500.75d),
new Employee("Pekka", 4500.55d),
new Employee("Noob", 1000.55d));
public static void main(String[] args) {
List<Employee> sortedList = employees.stream()
.sorted(Comparator.
comparingDouble(Employee::getSalary))
.collect(Collectors.toList());
System.out.println(sortedList);
}
static class Employee {
private String name;
private Double salary;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
public Employee(String name, Double salary) {
this.name = name;
this.salary = salary;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", salary=" + salary +
'}';
}
}
}
Console Output:
[Employee{name='Noob', salary=1000.55}, Employee{name='Gama', salary=1500.75}, Employee{name='Beta', salary=2500.5}, Employee{name='Alpha', salary=3000.45}, Employee{name='Pekka', salary=4500.55}]
1.2 Sort by salary, reverse order.
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class Main {
static List<Employee> employees = Arrays.asList(
new Employee("Alpha", 3000.45d),
new Employee("Beta", 2500.50d),
new Employee("Gama", 1500.75d),
new Employee("Pekka", 4500.55d),
new Employee("Noob", 1000.55d));
public static void main(String[] args) {
List<Employee> sortedList = employees.stream().
sorted(Comparator.comparingDouble
(Employee::getSalary).reversed()).
collect(Collectors.toList());
System.out.println(sortedList);
}
static class Employee {
private String name;
private Double salary;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
public Employee(String name, Double salary) {
this.name = name;
this.salary = salary;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", salary=" + salary +
'}';
}
}
}
Console Output:
[Employee{name='Pekka', salary=4500.55}, Employee{name='Alpha', salary=3000.45}, Employee{name='Beta', salary=2500.5}, Employee{name='Gama', salary=1500.75}, Employee{name='Noob', salary=1000.55}]