项目作者: VisionITTesting

项目描述 :
Sort Collections using Comparable and Comparator Interface
高级语言: Java
项目地址: git://github.com/VisionITTesting/tutorial-code-java-comparable-comparator.git
创建时间: 2021-02-05T06:33:35Z
项目社区:https://github.com/VisionITTesting/tutorial-code-java-comparable-comparator

开源协议:Apache License 2.0

下载


Code Example for How to Use Comparator and Comparable


  • Created by Akash Tyagi on 5thFeb2021
  • Detail: How to use Comparable and Comparator interface to sort the arrays with simple or custom objects.

Comparable Example:

  • Employee Class with compareTo method implemented
    ```java
    package comparable.example;

public class Employee implements Comparable{

  1. String name;
  2. int age;
  3. public Employee(String name, int age){
  4. this.name = name;
  5. this.age = age;
  6. }
  7. @Override
  8. public String toString(){
  9. return this.name + " " + this.age;
  10. }
  11. @Override
  12. public int compareTo(Employee o) {
  13. if (this.age<o.age){
  14. return -1;
  15. }else if (this.age==o.age){
  16. return 0;
  17. }else {
  18. return 1;
  19. }
  20. }

}

  1. * Comparator Class
  2. ```java
  3. package comparable.example;
  4. import java.util.ArrayList;
  5. import java.util.Collections;
  6. public class ComparableExample {
  7. public static void main(String[] args){
  8. Employee emp1 = new Employee("Akash",35);
  9. Employee emp2 = new Employee("SunShun",25);
  10. Employee emp3 = new Employee("Keerthana",26);
  11. Employee emp4 = new Employee("Timothy",29);
  12. Employee emp5 = new Employee("Maverick",38);
  13. //Create a Array list
  14. ArrayList<Employee> arrayList = new ArrayList();
  15. arrayList.add(emp1);
  16. arrayList.add(emp2);
  17. arrayList.add(emp3);
  18. arrayList.add(emp4);
  19. arrayList.add(emp5);
  20. //Sort the List
  21. Collections.sort(arrayList);
  22. System.out.println(arrayList);
  23. }
  24. }

Comparator Example for Sorting:

  • Employee Class:
  1. package comparator.example;
  2. public class Employee1 implements Comparable<Employee1>{
  3. String name;
  4. int age;
  5. int salary;
  6. public Employee1(String name, int age, int salary){
  7. this.name = name;
  8. this.age = age;
  9. this.salary = salary;
  10. }
  11. @Override
  12. public String toString(){
  13. return this.name + " " + this.age + " " + this.salary;
  14. }
  15. @Override
  16. public int compareTo(Employee1 o) {
  17. if (this.age<o.age){
  18. return -1;
  19. }else if (this.age==o.age){
  20. return 0;
  21. }else{
  22. return 1;
  23. }
  24. }
  25. }
  • Employee Age Comparator
  1. package comparator.example;
  2. import java.util.Comparator;
  3. public class EmployeeAgeComparator implements Comparator<Employee1> {
  4. @Override
  5. public int compare(Employee1 o1, Employee1 o2) {
  6. if (o1.age>o2.age){
  7. return 1;
  8. }else if (o1.age<o2.age){
  9. return -1;
  10. }else{
  11. return 0;
  12. }
  13. }
  14. }
  • Employee Salary Comparator
  1. package comparator.example;
  2. import java.util.Comparator;
  3. public class EmployeeSalaryComparator implements Comparator<Employee1> {
  4. @Override
  5. public int compare(Employee1 o1, Employee1 o2) {
  6. if (o1.salary>o2.salary){
  7. return 1;
  8. }else if (o1.salary<o2.salary){
  9. return -1;
  10. }else{
  11. return 0;
  12. }
  13. }
  14. }
  • Comparator Example, sort by Age and Salary
  1. package comparator.example;
  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. public class ComparatorExample {
  5. public static void main(String[] args){
  6. Employee1 emp1 = new Employee1("Akash",35,1500);
  7. Employee1 emp2 = new Employee1("SunSHun",25,3500);
  8. Employee1 emp3 = new Employee1("Tim",29,3000);
  9. Employee1 emp4 = new Employee1("Keerthana",26,500);
  10. Employee1 emp5 = new Employee1("Naverick",38,4000);
  11. //Create a Array list
  12. ArrayList<Employee1> arrayList = new ArrayList<Employee1>();
  13. arrayList.add(emp1);
  14. arrayList.add(emp2);
  15. arrayList.add(emp3);
  16. arrayList.add(emp4);
  17. arrayList.add(emp5);
  18. //Sort the array, by default based on age as mentioned in the Employee Class using Comparable
  19. Collections.sort(arrayList);
  20. System.out.println("Sorting based on Age: " + arrayList.toString());
  21. //Sort based on Salary Comparator
  22. Collections.sort(arrayList,new EmployeeSalaryComparator());
  23. System.out.println("Sorting based on Salary as: " + arrayList.toString());
  24. }
  25. }