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

更新日期: 30-Mar-2022

604 次浏览

启动你的 职业

完成课程以获得认证

开始
广告