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

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

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程