- vừa được xem lúc

Sắp xếp Collection trong Java

0 0 13

Người đăng: Phan Man

Theo Viblo Asia

1. Đối với array:

Để 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
7

Gó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 Comparator

Trướ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

Bình luận

Bài viết tương tự

- vừa được xem lúc

Giới thiệu Typescript - Sự khác nhau giữa Typescript và Javascript

Typescript là gì. TypeScript là một ngôn ngữ giúp cung cấp quy mô lớn hơn so với JavaScript.

0 0 500

- vừa được xem lúc

Cài đặt WSL / WSL2 trên Windows 10 để code như trên Ubuntu

Sau vài ba năm mình chuyển qua code trên Ubuntu thì thật không thể phủ nhận rằng mình đã yêu em nó. Cá nhân mình sử dụng Ubuntu để code web thì thật là tuyệt vời.

0 0 376

- vừa được xem lúc

Đặt tên commit message sao cho "tình nghĩa anh em chắc chắn bền lâu"????

. Lời mở đầu. .

1 1 701

- vừa được xem lúc

Tìm hiểu về Resource Controller trong Laravel

Giới thiệu. Trong laravel, việc sử dụng các route post, get, group để gọi đến 1 action của Controller đã là quá quen đối với các bạn sử dụng framework này.

0 0 335

- vừa được xem lúc

Phân quyền đơn giản với package Laravel permission

Như các bạn đã biết, phân quyền trong một ứng dụng là một phần không thể thiếu trong việc phát triển phần mềm, dù đó là ứng dụng web hay là mobile. Vậy nên, hôm nay mình sẽ giới thiệu một package có thể giúp các bạn phân quyền nhanh và đơn giản trong một website được viết bằng PHP với framework là L

0 0 421

- vừa được xem lúc

Bạn đã biết các tips này khi làm việc với chuỗi trong JavaScript chưa ?

Hi xin chào các bạn, tiếp tục chuỗi chủ đề về cái thằng JavaScript này, hôm nay mình sẽ giới thiệu cho các bạn một số thủ thuật hay ho khi làm việc với chuỗi trong JavaScript có thể bạn đã hoặc chưa từng dùng. Cụ thể như nào thì hãy cùng mình tìm hiểu trong bài viết này nhé (go).

0 0 414