[Java Cơ Bản] Sắp Xếp đối Tượng Trong Java Với Comparable Và ...
Có thể bạn quan tâm
Bài đăng nổi bật
Học Ngành Gì KHÔNG THẤT NGHIỆP
13:22 Trang chủJava [Java cơ bản] Sắp xếp đối tượng trong Java với Comparable và Comparator 19:54 0 Nhận xét Trong hướng dẫn này, chúng ta tìm hiểu cách sử dụng java.lang.Comparable và java.util.Comparator để sắp xếp một đối tượng Java theo giá trị thuộc tính của nó.1. Sắp xếp một mảng
Để sắp xếp một mảng, sử dụng Arrays.sort () String[] fruits = new String[] {"Pineapple","Apple", "Orange", "Banana"}; Arrays.sort(fruits); int i=0; for(String temp: fruits){ System.out.println("fruits " + ++i + " : " + temp); } Đầu ra fruits 1 : Apple fruits 2 : Banana fruits 3 : Orange fruits 4 : Pineapple2. Sắp xếp một ArrayList
Để sắp xếp một ArrayList, hãy sử dụng Collections.sort () . List<String> fruits = new ArrayList<String>(); fruits.add("Pineapple"); fruits.add("Apple"); fruits.add("Orange"); fruits.add("Banana"); Collections.sort(fruits); int i=0; for(String temp: fruits){ System.out.println("fruits " + ++i + " : " + temp); } Đầu ra fruits 1 : Apple fruits 2 : Banana fruits 3 : Orange fruits 4 : Pineapple3. Sắp xếp một đối tượng với so sánh
Làm thế nào về một đối tượng Java? Hãy tạo một lớp Fruit: public class Fruit{ private String fruitName; private String fruitDesc; private int quantity; public Fruit(String fruitName, String fruitDesc, int quantity) { super(); this.fruitName = fruitName; this.fruitDesc = fruitDesc; this.quantity = quantity; } public String getFruitName() { return fruitName; } public void setFruitName(String fruitName) { this.fruitName = fruitName; } public String getFruitDesc() { return fruitDesc; } public void setFruitDesc(String fruitDesc) { this.fruitDesc = fruitDesc; } public int getQuantity() { return quantity; } public void setQuantity(int quantity) { this.quantity = quantity; } } Để sắp xếp nó, bạn có thể nghĩ lại Arrays.sort () , xem ví dụ dưới đây: import java.util.Arrays; public class SortFruitObject{ public static void main(String args[]){ Fruit[] fruits = new Fruit[4]; Fruit pineappale = new Fruit("Pineapple", "Pineapple description",70); Fruit apple = new Fruit("Apple", "Apple description",100); Fruit orange = new Fruit("Orange", "Orange description",80); Fruit banana = new Fruit("Banana", "Banana description",90); fruits[0]=pineappale; fruits[1]=apple; fruits[2]=orange; fruits[3]=banana; Arrays.sort(fruits); int i=0; for(Fruit temp: fruits){ System.out.println("fruits " + ++i + " : " + temp.getFruitName() + ", Quantity : " + temp.getQuantity()); } } } Hãy thử, nhưng, những gì bạn mong đợi Arrays.sort () sẽ làm gì? Bạn thậm chí không đề cập đến những gì để sắp xếp trong lớp trái cây. Vì vậy, nó sẽ gặp lỗi sau: Exception in thread "main" java.lang.ClassCastException: Để sắp xếp một Object theo thuộc tính của nó, bạn phải làm cho Object đó implements interface Comparable và ghi đè phương thức compareTo(). Hãy quan sát lớp Fruit mới một lần nữa. public class Fruit implements Comparable<Fruit>{ private String fruitName; private String fruitDesc; private int quantity; public Fruit(String fruitName, String fruitDesc, int quantity) { super(); this.fruitName = fruitName; this.fruitDesc = fruitDesc; this.quantity = quantity; } public String getFruitName() { return fruitName; } public void setFruitName(String fruitName) { this.fruitName = fruitName; } public String getFruitDesc() { return fruitDesc; } public void setFruitDesc(String fruitDesc) { this.fruitDesc = fruitDesc; } public int getQuantity() { return quantity; } public void setQuantity(int quantity) { this.quantity = quantity; } public int compareTo(Fruit compareFruit) { int compareQuantity = ((Fruit) compareFruit).getQuantity(); //ascending order return this.quantity - compareQuantity; //descending order //return compareQuantity - this.quantity; } } Lớp Fruit mới đã implement interface Comparable và ghi đè phương thức compareTo() để so sánh thuộc tính quantity của nó theo thứ tự tăng dần. Chạy lại chương trình, bây giờ mảng Fruits được sắp xếp theo số lượng theo thứ tự tăng dần. fruits 1 : Pineapple, Quantity : 70 fruits 2 : Orange, Quantity : 80 fruits 3 : Banana, Quantity : 90 fruits 4 : Apple, Quantity : 1004. Sắp xếp đối tượng với Comparator
Làm thế nào để sắp xếp Fruit theo fruitName hoặc quantity? Interface Comparable chỉ cho phép sắp xếp theo một thuộc tính nào đó. Để sắp xếp theo nhiều thuộc tính, bạn cần đến interface Comparator. Quan sát sự cập nhập lại lớp Fruit một lần nữa: import java.util.Comparator; public class Fruit implements Comparable<Fruit>{ private String fruitName; private String fruitDesc; private int quantity; public Fruit(String fruitName, String fruitDesc, int quantity) { super(); this.fruitName = fruitName; this.fruitDesc = fruitDesc; this.quantity = quantity; } public String getFruitName() { return fruitName; } public void setFruitName(String fruitName) { this.fruitName = fruitName; } public String getFruitDesc() { return fruitDesc; } public void setFruitDesc(String fruitDesc) { this.fruitDesc = fruitDesc; } public int getQuantity() { return quantity; } public void setQuantity(int quantity) { this.quantity = quantity; } public int compareTo(Fruit compareFruit) { int compareQuantity = ((Fruit) compareFruit).getQuantity(); //ascending order return this.quantity - compareQuantity; //descending order //return compareQuantity - this.quantity; } public static Comparator<Fruit> FruitNameComparator = new Comparator<Fruit>() { public int compare(Fruit fruit1, Fruit fruit2) { String fruitName1 = fruit1.getFruitName().toUpperCase(); String fruitName2 = fruit2.getFruitName().toUpperCase(); //ascending order return fruitName1.compareTo(fruitName2); //descending order //return fruitName2.compareTo(fruitName1); } }; } Lớp Fruit chứa phương thức tĩnh là FruitNameComparator để so sánh theo FruitName. Bây giờ, đối tượng Fruit đã có thể sắp xếp theo quantity hoặc fruitName. Hãy chạy lại chương trình và quan sát kết quả. VD1. Sắp xếp mảng Fruit theo thứ tự tăng dần dựa trên thuộc tính fruitName. Arrays.sort(fruits, Fruit.FruitNameComparator); Đầu ra fruits 1 : Apple, Quantity : 100 fruits 2 : Banana, Quantity : 90 fruits 3 : Orange, Quantity : 80 fruits 4 : Pineapple, Quantity : 70 VD2. Sắp xếp mảng Fruit theo thứ tự tăng dần dựa trên thuộc tính quantity. Arrays.sort(fruits) Đầu ra fruits 1 : Pineapple, Quantity : 70 fruits 2 : Orange, Quantity : 80 fruits 3 : Banana, Quantity : 90 fruits 4 : Apple, Quantity : 100 Interface java.lang.Comparable và java.util.Comparator hỗ trợ việc sắp xếp rất tốt, tuy nhiên cần nhiều thời gian để tìm hiểu về cách sử dụng nó. Java ProgrammingYou might like
Hiện thêmPost a Comment
Đăng nhận xét
Mới hơn Cũ hơnFollow Us
Chủ Đề Tôi Quan Tâm ;)
- .Net
- .net core
- Agile
- AI
- AngularJS
- Anroid
- AP
- Azure
- BDW
- Bootstrap
- c
- C#
- Cấu Trúc Dữ Liệu và Giải Thuật
- CodeLean
- CSDL
- CSS
- Database
- Design Pattern
- DesignThinking
- DevOps
- Docker
- Git
- github
- HTML
- Hướng_Nghiệp
- Index
- ISA
- Java
- Java1
- Java2
- JavaAdvanced
- JavaCore
- JavaScript
- JSP & Servlet
- Laravel Framework
- Laravel9
- Lập trình C
- Lập trình cho trẻ
- LINQ
- MLJ
- MySQL
- NodeJS
- OOAD
- PHP
- Programming
- Python
- QA
- React Native
- Review Sách
- Spring Boot
- Spring Framework
- SQL
- Teaching
- Test
- Thiết Kế Website
- Tools
- TypeScript
- Web Development
- WebAPI
Most Popular
Chuẩn hoá dữ liệu là gì? 1NF, 2NF, 3NF & BCNF với các ví dụ
11:57[MySQL 05] Mô hình ER là gì và ví dụ
12:08Tìm Phủ Tối thiểu của một Hàm
12:29Xác định khóa, Tìm phủ tối thiểu, Chuẩn hóa dữ liệu
16:36Big O: Cách tính độ phức tạp của thời gian và không gian
16:37Bắt đầu học Node.js với Visual Studio Code
10:07[Web API] Hướng dẫn từ cơ bản tới nâng cao Web API ASP.NET
19:21[MySQL 02] Hướng dẫn sử dụng MySQL Workbench
11:32Hướng dẫn sửa cấu hình để chạy Dự Án Laravel từ Source Code có sẵn
14:59[JSP & Servlet] Tạo Web Application JSP Servlet Sử dụng JSTL với Tomcat 10 trên IntelliJ
21:27Biểu mẫu liên hệ
Từ khóa » Ham Sắp Xếp Arraylist Trong Java
-
Sort Tăng Và Giảm Dần Trong ArrayList - Deft Blog
-
Bài 17: Sắp Xếp Các đối Tượng Trong ArrayList - Lập Trình Android
-
Phương Thức Sort() Trong ArrayList Java
-
Java Collections: Sắp Xếp Collections - Viblo
-
Sắp Xếp Trong Java 8 - Viblo
-
Sorting Trong Collections - VietTuts
-
Làm Thế Nào để Sắp Xếp Một ArrayList? - HelpEx
-
Java: Sắp Xếp Trong Collection | V1Study
-
Các Cách Tiếp Cận Khác Nhau để Sắp Xếp Các Phần Tử Của ArrayList ...
-
Cách Sắp Xếp Danh Sách Theo Thứ Tự / How To Sort A List/Array By ...
-
Java — Sắp Xếp Các đối Tượng Trong ArrayList Theo Ngày?
-
So Sánh Và Sắp Xếp Trong Java - Openplanning
-
Tất Tần Tật Về Java Collections - Sorting (Phần 7) - Techmaster