Java 程序按属性对自定义对象的 ArrayList 进行排序

javacampus interviewserver side programmingprogramming更新于 2024/8/8 18:53:00

在本文中,我们将了解如何按属性对自定义对象的 arrayList 进行排序。ArrayList 类扩展了 AbstractList 并实现了 List 接口。ArrayList 支持可以根据需要增长的动态数组。

数组列表以初始大小创建。当超出此大小时,集合会自动扩大。当删除对象时,数组可能会缩小。

下面是相同的演示 −

假设我们的输入是

列表定义为
Java
Scala
Python
Mysql

期望输出将是

对值进行排序后的列表:
Java
Mysql
Python
Scala

算法

步骤 1 - 开始
步骤 2 - 声明
步骤 3 - 定义值。
步骤 4 - 使用 ‘sort’ 方法对列表进行排序。
步骤 5 - 使用 ‘compareTo’ 方法比较列表的属性。
步骤 6 - 使用 ‘add’ 方法将新值添加到列表中。
步骤 7 - 在主方法中,创建一个数组列表,并调用 ‘sort’ 方法。
步骤 8 - 显示结果
步骤 9 - 停止

示例 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

相关文章