Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Java

Chương trình Java sắp xếp ArrayList đối tượng tùy chỉnh (Custom Objects) theo thuộc tính


Trong bài viết này, chúng ta sẽ hiểu cách sắp xếp ArrayList các đối tượng tùy chỉnh (custom objects) theo thuộc tính (property). Lớp ArrayList kế thừa AbstractList và triển khai interface List. ArrayList hỗ trợ mảng động có thể mở rộng khi cần.

ArrayList được tạo với kích thước ban đầu. Khi vượt quá kích thước này, collection tự động mở rộng. Khi xóa đối tượng, mảng có thể thu nhỏ.

Minh họa dưới đây:

Giả sử đầu vào là:

The list is defined as Java Scala Python Mysql

Đầu ra mong muốn:

The list after sorting values: Java Mysql Python Scala

Thuật toán

Bước 1 - BẮT ĐẦU 
Bước 2 - Khai báo biến 
Bước 3 - Định nghĩa giá trị 
Bước 4 - Dùng 'sort' để sắp xếp list 
Bước 5 - Dùng 'compareTo' so sánh thuộc tính list 
Bước 6 - Dùng 'add' thêm giá trị mới vào list 
Bước 7 - Trong main, tạo array list, gọi 'sort' 
Bước 8 - Hiển thị kết quả 
Bước 9 - DỪNG

Ví dụ 1

Gom thao tác dưới 'main'.

import java.util.*; 

class CustomObject { 
    private String custom_property; 
    public CustomObject(String property){ 
        this.custom_property = property; 
    } 
    public String get_custom_property(){ 
        return this.custom_property; 
    } 
} 

public class Demo { 
    public static void print(ArrayList input_list){ 
        for (CustomObject object : input_list) { 
            System.out.println(object.get_custom_property()); 
        } 
    } 
    
    public static void sort(ArrayList input_list){ 
        input_list.sort((object_1, object_2) 
            -> object_1.get_custom_property().compareTo( 
                object_2.get_custom_property())); 
    } 
    
    public static void add(ArrayList input_list){ 
        input_list.add(new CustomObject("Java")); 
        input_list.add(new CustomObject("Scala")); 
        input_list.add(new CustomObject("Python")); 
        input_list.add(new CustomObject("Mysql")); 
    } 
    
    public static void main(String[] args){ 
        System.out.println("Required packages have been imported"); 
        ArrayList input_list = new ArrayList<>(); 
        add(input_list); 
        System.out.println("The list is defined as "); 
        print(input_list); 
        sort(input_list); 
        System.out.println("\nThe list after sorting values: "); 
        print(input_list); 
    } 
}

Kết quả

Required packages have been imported 
The list is defined as 
Java 
Scala 
Python 
Mysql 
The list after sorting values: 
Java 
Mysql 
Python 
Scala

Ví dụ 2

Đóng gói thao tác vào hàm thể hiện lập trình hướng đối tượng.

import java.util.*; 

class CustomObject { 
    private String custom_property; 
    public CustomObject(String property){ 
        this.custom_property = property; 
    } 
    public String get_custom_property(){ 
        return this.custom_property; 
    } 
} 

public class Demo { 
    public static void main(String[] args){ 
        System.out.println("Required packages have been imported"); 
        ArrayList input_list = new ArrayList<>(); 
        input_list.add(new CustomObject("Java")); 
        input_list.add(new CustomObject("Scala")); 
        input_list.add(new CustomObject("Python")); 
        input_list.add(new CustomObject("Mysql")); 
        System.out.println("The number is defined as "); 
        for (CustomObject object : input_list) { 
            System.out.println(object.get_custom_property()); 
        } 
        input_list.sort((object_1, object_2) 
            -> object_1.get_custom_property().compareTo( 
                object_2.get_custom_property())); 
        System.out.println("\nThe list after sorting values: "); 
        for (CustomObject object : input_list) { 
            System.out.println(object.get_custom_property()); 
        } 
    } 
}

Kết quả

Required packages have been imported 
The number is defined as 
Java 
Scala 
Python 
Mysql 
The list after sorting values: 
Java 
Mysql 
Python 
Scala