Sắp Xếp Collection Trong Java - Viblo
Có thể bạn quan tâm
Để sort một array, chúng ta có thể dùng method sort trong gói java.util.Arrays Ví dụ:
int [] array = new int[]{1, 2, 4, 1, 3, 5, 7, 6}; Arrays.sort(array); for (int x : array) { System.out.println(x); }Kết quả:
1 1 2 3 4 5 6 7Gói Java.util.Arrays còn hỗ trợ 1 số phương thức sort khác như sau:
static void sort(byte[] a) static void sort(byte[] a, int fromIndex, int toIndex) static void sort(char[] a) static void sort(char[] a, int fromIndex, int toIndex) static void sort(double[] a) static void sort(double[] a, int fromIndex, int toIndex) static void sort(float[] a) static void sort(float[] a, int fromIndex, int toIndex) static void sort(int[] a) static void sort(int[] a, int fromIndex, int toIndex) static void sort(long[] a) static void sort(long[] a, int fromIndex, int toIndex) static void sort(Object[] a) static void sort(Object[] a, int fromIndex, int toIndex) static void sort(short[] a) static void sort(short[] a, int fromIndex, int toIndex) static <T> void sort(T[] a, Comparator<? super T> c) static <T> void sort(T[] a, int fromIndex, int toIndex, Comparator<? super T> c) 2. Đối với List List<Integer> list = new ArrayList<Integer>(Arrays.asList(1, 2, 4, 1, 3, 5, 7, 6)); Collections.sort(list); list.forEach(e -> System.out.println(e));Kết quả:
1 1 2 3 4 5 6 7 3. Sắp xếp list objects sử dụng ComparatorTrước hết, chúng ta tạo class Student như sau:
public class Student { private String name; private int age; public Student() { } public Student(String name,int age ) { this.name = name; this.age = age; } 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; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; } }3.1 Implement interface Comparator<T>
Interface Comparable<T> cung cấp một phương thức để sắp xếp list object là int compare(T o1, T o2). Việc chúng ta chỉ là implement interface này.
public class Student implements Comparator<Student> { // Các method và field như ở trên @Override public int compare(Student o1, Student o2) { return o1.getAge() - o2.getAge(); } }Và gọi java.util.Collections.sort để sắp xếp.
List<Student> students = new ArrayList<>(); students.add(new Student("Jonh", 17)); students.add(new Student("Peter", 19)); students.add(new Student("Henry", 18)); java.util.Collections.sort(students, new Student()); students.forEach(e -> System.out.println(e));Kết quả:
Student{name='Jonh', age=17} Student{name='Henry', age=18} Student{name='Peter', age=19}##3.2 Sử dụng anonymous comparator để so sánh Thay vì implement trực tiếp từ interface Comparator<T>, chúng ta có thể sử dụng anonymous comparator như sau:
List<Student> students = new ArrayList<>(); students.add(new Student("Jonh", 17)); students.add(new Student("Peter", 19)); students.add(new Student("Henry", 18)); Collections.sort(students, new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { return o1.getAge() - o2.getAge(); } }); students.forEach(e -> System.out.println(e));Kết quả vẫn không đổi:
Student{name='Jonh', age=17} Student{name='Henry', age=18} Student{name='Peter', age=19} 4. Sắp xếp list objects sử dụng Comparable<T>Vẫn sử dụng class Student ở trên, nhưng chúng ta sẽ implements interface Comparable<Student> và override lại method compareTo(Student o)
@Override public int compareTo(Student o) { return this.getAge() - o.getAge(); } List<Student> students = new ArrayList<>(); students.add(new Student("Jonh", 17)); students.add(new Student("Peter", 19)); students.add(new Student("Henry", 18)); Collections.sort(students); students.forEach(e -> System.out.println(e));Kết quả:
Student{name='Jonh', age=17} Student{name='Henry', age=18} Student{name='Peter', age=19}Tham khảo: https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html
Từ khóa » Hàm Sắp Xếp Mảng Trong Java
-
Sắp Xếp Mảng Trong Java
-
Bài Tập Java - Sắp Xếp Mảng Theo Thứ Tự Tăng Dần - VietTuts
-
Cách Sắp Xếp Một Mảng Trong Java - TutorialCup
-
Sắp Xếp Mảng Số Nguyên Trong Java | Tìm ở đây
-
Sắp Xếp Tăng Dần Và Giảm Dần Trong Java - Freetuts
-
Sắp Xếp Trong Java - KungFu Tech
-
Bài Tập Java Cơ Bản: Sắp Xếp Mảng Theo Thứ Tự Tăng Dần | Codelearn
-
Sắp Xếp Tăng Dần Và Giảm Dần Trong Java - Bài Tập Java Có Lời Giải ...
-
Làm Thế Nào để Sắp Xếp Array Trong Java? - CodeGym
-
So Sánh Và Sắp Xếp Trong Java - Openplanning
-
Java 55. Tìm Kiếm, Sắp Xếp, Copy Mảng Bằng Các Hàm Có Sẵn Trong ...
-
Java: Sắp Xếp Trong Collection | V1Study
-
Cách Thức Hoạt động Của Thuật Toán Sắp Xếp Chèn Trong Java Và C ++
-
Sắp Xếp Mảng Tăng Dần Java