Learn Java Lambda at https://www.javaguides.net/2018/07/java-8-lambda-expressions.html.
Sort List of integers using Collections.sort() method
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class SortList {
public static void main(String[] args) {
// create list
List < Integer > list = new ArrayList < Integer > ();
list.add(10);
list.add(30);
list.add(20);
list.add(50);
list.add(40);
Collections.sort(list); // ascending order
System.out.println(list);
Collections.reverse(list); // descending order
System.out.println(list);
}
}
[10, 20, 30, 40, 50]
[50, 40, 30, 20, 10]
Sort List of Employee Objects in Ascending and Descending Order using Comparator
package com.java.tutorials.sorting;
public class Employee {
private int id;
private String name;
private int age;
private long salary;
public Employee(int id, String name, int age, long salary) {
super();
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public long getSalary() {
return salary;
}
public void setSalary(long salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", age=" + age + ", salary=" + salary + "]";
}
}
class MySort implements Comparator < Employee > {
@Override
public int compare(Employee o1, Employee o2) {
return (int)(o1.getSalary() - o2.getSalary());
}
}
package com.java.tutorials.sorting;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class SortList {
public static void main(String[] args) {
List < Employee > employees = new ArrayList < Employee > ();
employees.add(new Employee(10, "Ramesh", 30, 400000));
employees.add(new Employee(20, "Santosh", 29, 350000));
employees.add(new Employee(30, "Sanjay", 30, 450000));
employees.add(new Employee(40, "Pramod", 29, 500000));
// ascending order
Collections.sort(employees, new MySort());
System.out.println(employees);
}
}
[Employee [id=20, name=Santosh, age=29, salary=350000], Employee [id=10, name=Ramesh, age=30, salary=400000], Employee [id=30, name=Sanjay, age=30, salary=450000], Employee [id=40, name=Pramod, age=29, salary=500000]]
class MySort implements Comparator < Employee > {
@Override
public int compare(Employee o1, Employee o2) {
return (int)(o2.getSalary() - o1.getSalary());
}
}
return (int)(o2.getSalary() - o1.getSalary());
Sort List of Employee Objects in Ascending and Descending Order using Lambda Expressions
package com.java.tutorials.sorting;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class SortList {
public static void main(String[] args) {
List < Employee > employees = new ArrayList < Employee > ();
employees.add(new Employee(10, "Ramesh", 30, 400000));
employees.add(new Employee(20, "Santosh", 29, 350000));
employees.add(new Employee(30, "Sanjay", 30, 450000));
employees.add(new Employee(40, "Pramod", 29, 500000));
Collections.sort(employees, new Comparator < Employee > () {
@Override
public int compare(Employee o1, Employee o2) {
return (int)(o1.getName().compareTo(o2.getName()));
}
});
// using lambda expression
// ascending order
Collections.sort(employees, (o1, o2) -> (o1.getName().compareTo(o2.getName())));
System.out.println("Ascending order => " + employees);
// descending order
Collections.sort(employees, (o1, o2) -> (o2.getName().compareTo(o1.getName())));
System.out.println("Descending order => " + employees);
// using Comparator.comparing() method
Collections.sort(employees, Comparator.comparing(Employee::getName));
}
}
class MySort implements Comparator < Employee > {
@Override
public int compare(Employee o1, Employee o2) {
return (int)(o1.getSalary() - o2.getSalary());
}
}
// using lambda expression
// ascending order
Collections.sort(employees, (o1, o2) -> (o1.getName().compareTo(o2.getName())));
System.out.println("Ascending order => " + employees);
// descending order
Collections.sort(employees, (o1, o2) -> (o2.getName().compareTo(o1.getName())));
System.out.println("Descending order => " + employees);
Ascending order => [Employee [id=40, name=Pramod, age=29, salary=500000], Employee [id=10, name=Ramesh, age=30, salary=400000], Employee [id=30, name=Sanjay, age=30, salary=450000], Employee [id=20, name=Santosh, age=29, salary=350000]]
Descending order => [Employee [id=20, name=Santosh, age=29, salary=350000], Employee [id=30, name=Sanjay, age=30, salary=450000], Employee [id=10, name=Ramesh, age=30, salary=400000], Employee [id=40, name=Pramod, age=29, salary=500000]]
Related Java 8 Tutorials
Continue Learning with Java Guides
Want to take your programming skills to the next level? Explore my practical Udemy courses on Java, Spring Boot, Microservices, Full-Stack Development, Docker, and Generative AI. You can also follow my structured learning roadmaps to become a professional developer.
By Ramesh Fadatare - Udemy Instructor Partner
Comments
Post a Comment
Leave Comment