Java 程式按屬性對自定義物件的 ArrayList 排序
在本文中,我們將瞭解如何按屬性對自定義物件的 arrayList 排序。ArrayList 類擴充套件了 AbstractList 並實現了 List 介面。ArrayList 支援按需增長的動態陣列。
建立具有初始大小的陣列列表。超過此大小時,該集合將自動擴大。移除物件時,陣列可能會縮小。
以下是同樣的演示 −
假設我們的輸入是 −
The list is defined as Java Scala Python Mysql
期望的輸出是 −
The list after sorting values: Java Mysql Python Scala
演算法
Step 1 - START Step 2 - Declare namely Step 3 - Define the values. Step 4 - Use the ‘sort’ method to sort the list. Step 5 - Use the ‘compareTo’ method to compare properties of the list. Step 6 - Use the ‘add’ method to add new values to the list. Step 7 - In the main method, create an array list, and invoke the ‘sort’ method. Step 8 - Display the result Step 9 - Stop
示例 1
在這裡,我們將所有操作繫結在一起,置於“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<CustomObject> input_list){ for (CustomObject object : input_list) { System.out.println(object.get_custom_property()); } } public static void sort(ArrayList<CustomObject> 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<CustomObject> 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<CustomObject> 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); } }
輸出
Required packages have been imported The list is defined as Java Scala Python Mysql The list after sorting values: Java Mysql Python Scala
示例 2
在這裡,我們將操作封裝到函式中,展示面向物件程式設計。
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<CustomObject> 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()); } } }
輸出
Required packages have been imported The number is defined as Java Scala Python Mysql The list after sorting values: Java Mysql Python Scala
廣告