Category : Java | Sub Category : Java8 Features | By Prasad Bonam Last updated: 2023-11-13 13:47:10 Viewed : 230
sort a list in Java 8:
To sort a list in Java 8, you can use the Collections.sort
method or the List.sort
method along with lambda expressions or method references for custom sorting. Here are examples using both approaches:
Using Collections.sort
:
javaimport java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ListSortingExample {
public static void main(String[] args) {
List<String> names = new ArrayList<>();
names.add("John");
names.add("Jane");
names.add("Bob");
names.add("Alice");
// Sorting using Collections.sort and lambda expression
Collections.sort(names, (s1, s2) -> s1.compareTo(s2));
// Output the sorted list
System.out.println("Sorted List: " + names);
}
}
Output:
lessSorted List: [Alice, Bob, Jane, John]
Using List.sort
:
javaimport java.util.ArrayList;
import java.util.List;
public class ListSortExample {
public static void main(String[] args) {
List<String> names = new ArrayList<>();
names.add("John");
names.add("Jane");
names.add("Bob");
names.add("Alice");
// Sorting using List.sort and method reference
names.sort(String::compareTo);
// Output the sorted list
System.out.println("Sorted List: " + names);
}
}
Output:
lessSorted List: [Alice, Bob, Jane, John]
In both examples, the list of names is sorted alphabetically. You can customize the sorting logic by providing your own comparator inside the Collections.sort
or List.sort
method. The examples above use the natural order by using String::compareTo
or (s1, s2) -> s1.compareTo(s2)
for sorting strings.
If you have a list of Employee
objects and you want to sort them based on the employee names in Java 8, you can use the List.sort
method along with a comparator. Here is an example:
javaimport java.util.ArrayList;
import java.util.List;
public class EmployeeSortingExample {
public static void main(String[] args) {
// Sample list of Employee objects
List<Employee> employees = new ArrayList<>();
employees.add(new Employee("John", 30));
employees.add(new Employee("Jane", 25));
employees.add(new Employee("Bob", 35));
employees.add(new Employee("Alice", 28));
System.out.println("Unsorted List: " + employees);
// Sorting the list by employee names
employees.sort((e1, e2) -> e1.getName().compareTo(e2.getName()));
System.out.println("Sorted List by Name: " + employees);
}
}
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;
}
@Override
public String toString() {
return "Employee{name= " + name + " , age=" + age + } ;
}
}
Output:
lessUnsorted List: [Employee{name= John , age=30}, Employee{name= Jane , age=25}, Employee{name= Bob , age=35}, Employee{name= Alice , age=28}]
Sorted List by Name: [Employee{name= Alice , age=28}, Employee{name= Bob , age=35}, Employee{name= Jane , age=25}, Employee{name= John , age=30}]
In this example:
Employee
class has a name
attribute.Employee
objects is sorted based on the names using the List.sort
method and a lambda expression for the comparator. The comparator compares the names using the compareTo
method.You can customize the comparator based on your specific sorting requirements.