📌  相关文章
📜  通过在Java中实现 Comparable 接口对自定义对象进行排序

📅  最后修改于: 2022-05-13 01:54:37.196000             🧑  作者: Mango

通过在Java中实现 Comparable 接口对自定义对象进行排序

Java提供了两个接口来使用类的数据成员对对象进行排序,即Comparable和 Comparator。在本文中,我们将重点介绍 Comparable 接口。 Comparable 对象能够通过自然排序将自己与另一个对象进行比较。该类必须实现Java.lang.Comparable 接口来比较其实例。

使用 Comparable 接口,我们可以排序:

  • 字符串对象
  • 包装类对象
  • 用户定义对象

句法

public interface Comparable 
{
    public int compareTo(T o);
}

其中T是要排序的 Object 的类型。

compareTo() 方法

对于任何支持排序的类,它应该实现 Comparable 接口并覆盖它的 compareTo() 方法。如果对象小于指定对象,则返回负整数,如果对象相等则返回零,如果对象大于指定对象,则返回正整数。

示例 1:根据学生分数对给定数据进行排序。

Java
// Java program to sort student
// data according to their marks
  
import java.util.*;
  
// implementing comparable interface
class StudentData implements Comparable {
  
    String name;
    int marks;
  
    // Constructor
    StudentData(String name, int marks)
    {
        this.name = name;
        this.marks = marks;
    }
  
    // overriding method to sort
    // the student data
    public int compareTo(StudentData sd)
    {
        return this.marks - sd.marks;
    }
}
  
// Driver class
class GFG {
    public static void main(String[] args)
    {
  
        ArrayList list
            = new ArrayList();
  
        // Inserting data
        list.add(new StudentData("Ram", 98));
        list.add(new StudentData("Shyam", 84));
        list.add(new StudentData("Lokesh", 90));
  
        Collections.sort(list);
  
        // Displaying data
        for (StudentData sd : list)
            System.out.println(sd.name + " " + sd.marks);
    }
}


Java
// Java program to sort student
// data according to their names
  
import java.util.*;
  
// implementing comparable interface
class StudentData implements Comparable {
  
    int roll;
    String name;
    int marks;
  
    // Constructor
    StudentData(int roll, String name, int marks)
    {
        this.roll = roll;
        this.name = name;
        this.marks = marks;
    }
  
    // overriding method to sort
    // the student data
    public int compareTo(StudentData sd)
    {
  
        // compareTo is a string method
        return this.name.compareTo(sd.name);
    }
}
  
// Driver class
class GFG {
    public static void main(String[] args)
    {
  
        ArrayList list
            = new ArrayList();
  
        // Inserting data
        list.add(new StudentData(1, "Ram", 98));
        list.add(new StudentData(2, "Shyam", 84));
        list.add(new StudentData(3, "Lokesh", 90));
  
        Collections.sort(list);
  
        // Displaying data
        for (StudentData sd : list)
            System.out.println(sd.roll + " " + sd.name + " "
                               + sd.marks);
    }
}


输出
Shyam 84
Lokesh 90
Ram 98

示例 2:根据名称对给定数据进行排序。

Java

// Java program to sort student
// data according to their names
  
import java.util.*;
  
// implementing comparable interface
class StudentData implements Comparable {
  
    int roll;
    String name;
    int marks;
  
    // Constructor
    StudentData(int roll, String name, int marks)
    {
        this.roll = roll;
        this.name = name;
        this.marks = marks;
    }
  
    // overriding method to sort
    // the student data
    public int compareTo(StudentData sd)
    {
  
        // compareTo is a string method
        return this.name.compareTo(sd.name);
    }
}
  
// Driver class
class GFG {
    public static void main(String[] args)
    {
  
        ArrayList list
            = new ArrayList();
  
        // Inserting data
        list.add(new StudentData(1, "Ram", 98));
        list.add(new StudentData(2, "Shyam", 84));
        list.add(new StudentData(3, "Lokesh", 90));
  
        Collections.sort(list);
  
        // Displaying data
        for (StudentData sd : list)
            System.out.println(sd.roll + " " + sd.name + " "
                               + sd.marks);
    }
}
输出
3 Lokesh 90
1 Ram 98
2 Shyam 84