Với các phần tử thuộc Set hoặc Map, chúng ta có thể sử dụng TreeSet hoặc TreeMap để sắp xếp. Tuy nhiên, không thể sắp xếp phần tử của List mà phải sử dụng class Collections để sắp xếp các phần tử của List

Nếu bạn chưa xem các phần trước, có thể tham khảo tại đây:
Tất tần tật về Java Collections - Collections Framework (Phần 1)
Tất tần tật về Java Collections - List Interface (Phần 2)
Tất tần tật về Java Collections - Queue Interface (Phần 3)
Tất tần tật về Java Collections - Set Interface (Phần 4)
Tất tần tật về Java Collections - Map Interface (Phần 5)
Tất tần tật về Java Collections - Deque Interface và ArrayDeque (Phần 6)

Lớp Collections

Lớp Collections là một thành phần trong Java Collection Framework, nó cung cấp phương thức sort() để sắp xếp các phần tử của collection.

Hãy cùng xem ví dụ dưới đây:

import java.util.ArrayList;
import java.util.Collections;

public class Main {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("John");
        list.add("Victor");
        list.add("Anna");
        list.add("Chris");

        System.out.println("Danh sách ban đầu: ");
        System.out.println(list);

        //Sắp xếp với Collections.sort()
        Collections.sort(list);
        System.out.println("Danh sách sau khi sắp xếp: ");
        System.out.println(list);
    }
}

Kết quả:

Danh sách ban đầu:
[John, Victor, Anna, Chris]
Danh sách sau khi sắp xếp:
[Anna, Chris, John, Victor]

Comparable interface

Comparable interface sử dụng để sắp xếp các đối tượng của lớp do người dùng từ định nghĩa.

Trong interface này chỉ có duy nhất một phương thức là compareTo(), phương thức này được sử dụng để sắp xếp đối tượng hiện tạo với đối tượng được chỉ định

Giờ mình sẽ áp dung để sắp xếp danh sách học sinh theo tên:

public class Student implements Comparable<Student>{
    private String name;
    private double point;

    public Student(String name, double point) {
        this.name = name;
        this.point = point;
    }

    public String getName() {
        return name;
    }

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

    public double getPoint() {
        return point;
    }

    public void setPoint(double point) {
        this.point = point;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", point=" + point +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        return this.getName().compareTo(o.getName());
    }
}
import java.util.ArrayList;
import java.util.Collections;

public class Main {
    public static void main(String[] args) {
       ArrayList<Student> students = new ArrayList<>();
       students.add(new Student("John", 8.5));
       students.add(new Student("Anna", 9));
       students.add(new Student("Victor", 7));
       students.add(new Student("Chris", 6.5));

        System.out.println("Danh sách học sinh: ");
        show(students);

        Collections.sort(students);
        System.out.println("Danh sách sau khi sắp xếp: ");
        show(students);
    }

    public static void show(ArrayList<Student> list){
        for (Student student: list) {
            System.out.println(student);
        }
    }
}

Kết quả nhận được:

Danh sách học sinh:
Student{name='John', point=8.5}
Student{name='Anna', point=9.0}
Student{name='Victor', point=7.0}
Student{name='Chris', point=6.5}
Danh sách sau khi sắp xếp:
Student{name='Anna', point=9.0}
Student{name='Chris', point=6.5}
Student{name='John', point=8.5}
Student{name='Victor', point=7.0}

Có thể thấy danh sách được sắp xếp theo tên

Comparator interface

Tương tự Comparable, Comparator interface cũng được sử dụng để sắp xếp đối tượng của lớp do người dùng định nghĩa. Nó có 2 phương thức là compare() và equals().

Khác với Comparable, để sắp xếp một List, chúng ta cần tạo một lớp vô danh java.util.Comparator để cài đặt phương thức compare().

Hãy cùng xem ví dụ dưới đây:

public class Student {
    private String name;
    private double point;

    public Student(String name, double point) {
        this.name = name;
        this.point = point;
    }

    public String getName() {
        return name;
    }

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

    public double getPoint() {
        return point;
    }

    public void setPoint(double point) {
        this.point = point;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", point=" + point +
                '}';
    }
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class Main {
    public static void main(String[] args) {
        ArrayList<Student> students = new ArrayList<>();
        students.add(new Student("John", 8.5));
        students.add(new Student("Anna", 9));
        students.add(new Student("Victor", 7));
        students.add(new Student("Chris", 6.5));

        System.out.println("Danh sách học sinh: ");
        show(students);

        //Sắp xếp theo tên
        Collections.sort(students, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                //Cài đặt chỉ tiêu để so sánh trong đây
                return o1.getName().compareTo(o2.getName());
            }
        });
        System.out.println("Danh sách sắp xếp theo tên: ");
        show(students);

        //Sắp xếp theo điểm
        Collections.sort(students, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                //Sử dụng toán tử 3 ngôi
                return o1.getPoint() - o2.getPoint() > 0 ? 1 : -1;
            }
        });
        System.out.println("Danh sách sắp xếp theo điểm: ");
        show(students);
    }

    public static void show(ArrayList<Student> list){
        for (Student student: list) {
            System.out.println(student);
        }
    }
}

Kết quả:

Danh sách học sinh:
Student{name='John', point=8.5}
Student{name='Anna', point=9.0}
Student{name='Victor', point=7.0}
Student{name='Chris', point=6.5}
Danh sách sắp xếp theo tên:
Student{name='Anna', point=9.0}
Student{name='Chris', point=6.5}
Student{name='John', point=8.5}
Student{name='Victor', point=7.0}
Danh sách sắp xếp theo điểm:
Student{name='Chris', point=6.5}
Student{name='Victor', point=7.0}
Student{name='John', point=8.5}
Student{name='Anna', point=9.0}

Trong ví dụ trên, mình tạo 2 class vô danh của Comparator để sắp xếp đối tượng theo 2 tiêu chí name và point

So sánh Comparable và Comparator

image

ComparableComparator
Phải implements Comparable interface cho lớp đối tượng cần được sắp xếpKhông phải implements Comparator interface cho lớp đối tượng cần sắp xếp
Cung cấp phương thức compareTo() để sắp xếp các phần tửCung cấp phương thức compare() để sắp xếp phần tử
Nằm trong package java.langNằm trong package java.util
Sử dụng Collections.sort(List<T> list) để sắp xếp các phần tửSử dụng Collections.sort(List<T> list, Comparator<? super T> c) để sắp xếp các phần tử