📜  Java中已经实现 Comparable 的数组和包装类的元素排序

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

Java中已经实现 Comparable 的数组和包装类的元素排序

Java提供了 Comparable 接口来使用类的数据成员对对象进行排序。 Comparable 接口仅包含一个方法compareTo() ,该方法比较两个对象以在它们之间施加顺序。它返回一个负整数,零或正整数,以指示如果输入对象是小于等于或大于所述当前对象越大。它主要用于对自定义对象的数组或列表进行排序。

由于所有 Wrapper 类都已经实现了Java Comparable 接口,因此它提供了compareTo()的默认实现,这就是为什么可以在这些对象上使用Collections.sort()Arrays.sort()函数的原因。将包含 Wrapper 类的数组和列表元素排序为已经实现 Comparable 接口的对象。

插图:

Input  :  {8 , 9 , 1 , 5 , 3 , 0}
Output :  {0 , 1 , 3 , 5 , 8 , 9}


Input  :  {"Ankit", "Parul" , "Swati" , "Tarun", "Akshat"}
Output :  {"Akshat" , "Ankit" , "Parul" , "Swati" , "Tarun"}

执行:

例子



Java
// Java Program to Sorting Elements illustrating
// Array.srt) and Collection.sort() method
// Naive approach
 
// Importing all classes of
// java.util package
import java.util.*;
 
// Class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Case 1: Array of Integer using sort()
 
        // Sorting array of integers
        // using Arrays.sort()
        // Custom input entries
        int[] a = { 8, 9, 1, 5, 3, 0 };
 
        // Print the array before sorting
        System.out.println("Before Sorting: "
                           + Arrays.toString(a));
 
        // By default sorting is in ascending order
        Arrays.sort(a);
 
        // Print the array after sorting
        System.out.println("After Sorting: "
                           + Arrays.toString(a));
 
        // Case 2: Array of string using sort()
 
        // Sorting array of Strings
        // using Arrays.sort()
        String[] str = { "Ankit", "Parul", "Swati", "Tarun",
                         "Akshat" };
 
        // Print the input array of string before sorting
        System.out.println("Before Sorting: "
                           + Arrays.toString(str));
 
        // Sort() method
        Arrays.sort(str);
 
        // Print the input array of string before sorting
        System.out.println("After Sorting: "
                           + Arrays.toString(str));
 
        // Case 3: Collectios.sort
 
        // Sorting List of String Collections.sort()
 
        // Creating an list object of string type
        // Custominput elements in object
        List lt = Arrays.asList("Red", "Blue",
                                        "Green", "Black");
 
        // Print the elements before sorting
        System.out.println("Before Sorting:  " + lt);
 
        Collections.sort(lt);
 
        // Print the elements after sorting
        System.out.println("After Sorting:: " + lt);
    }
}


Java
// Java Program to Sorting Elements of Arrays and Wrapper
// Classes that Already Implements Comparable
 
// Importing all classes from
// java.util package
import java.util.*;
 
// Class 1
// Helper class
class Student implements Comparable {
 
    // Member variables of this class
    private int rollno;
    private String name;
    private Double marks;
 
    // Constructor of this class
    public Student(int rollno, String name, Double marks)
    {
        // This keyword refers to
        // current object itself
 
        this.rollno = rollno;
        this.name = name;
        this.marks = marks;
    }
 
    // Sorting based on marks of Students
    public int compareTo(Student s)
    {
        return this.marks.compareTo(s.marks);
    }
 
    public String toString()
    {
        return "Student{"
            + "RollNo=" + rollno + ", Name='" + name + '\''
            + ", Marks=" + marks + '}';
    }
}
 
// Class 2
// Main class
class GFG {
 
    // main driver method
    public static void main(String args[])
    {
        // Creating an ArrayList of user-defined
        // type(Student)
        ArrayList arr = new ArrayList<>();
 
        // Adding elements to object created above
        // Custom input entries
        arr.add(new Student(1, "Ankush", 98.0));
        arr.add(new Student(2, "Akshat", 99.0));
        arr.add(new Student(3, "Parul", 87.0));
        arr.add(new Student(4, "Tarun", 78.0));
        arr.add(new Student(5, "Swati", 90.0));
 
        // Iteratig over the above ArrayList
        for (int i = 0; i < arr.size(); i++)
 
            // Print the ArrayList elements as
            // in order added
            System.out.println(arr.get(i));
 
        // Calling the Collection.sort() method
        // to sort elements od ArrayList
        Collections.sort(arr);
 
        // Printing the ArrayList after sorting
        System.out.println("\nAfter Sorting :\n");
        for (int i = 0; i < arr.size(); i++)
            System.out.println(arr.get(i));
    }
}


输出
Before Sorting: [8, 9, 1, 5, 3, 0]
After Sorting: [0, 1, 3, 5, 8, 9]
Before Sorting: [Ankit, Parul, Swati, Tarun, Akshat]
After Sorting: [Akshat, Ankit, Parul, Swati, Tarun]
Before Sorting:  [Red, Blue, Green, Black]
After Sorting:: [Black, Blue, Green, Red]

现在,如果我们想将用户定义的类按特定顺序排序,那么我们必须实现存在于Java.lang包中的 Comparable 接口并提供compareTo( ) 方法的实现。如果指定为 null 或指定对象的类型阻止它与对象进行比较,compareTo 方法也会抛出 NullPointerException 或 ClassCastException。

执行:

例子

Java

// Java Program to Sorting Elements of Arrays and Wrapper
// Classes that Already Implements Comparable
 
// Importing all classes from
// java.util package
import java.util.*;
 
// Class 1
// Helper class
class Student implements Comparable {
 
    // Member variables of this class
    private int rollno;
    private String name;
    private Double marks;
 
    // Constructor of this class
    public Student(int rollno, String name, Double marks)
    {
        // This keyword refers to
        // current object itself
 
        this.rollno = rollno;
        this.name = name;
        this.marks = marks;
    }
 
    // Sorting based on marks of Students
    public int compareTo(Student s)
    {
        return this.marks.compareTo(s.marks);
    }
 
    public String toString()
    {
        return "Student{"
            + "RollNo=" + rollno + ", Name='" + name + '\''
            + ", Marks=" + marks + '}';
    }
}
 
// Class 2
// Main class
class GFG {
 
    // main driver method
    public static void main(String args[])
    {
        // Creating an ArrayList of user-defined
        // type(Student)
        ArrayList arr = new ArrayList<>();
 
        // Adding elements to object created above
        // Custom input entries
        arr.add(new Student(1, "Ankush", 98.0));
        arr.add(new Student(2, "Akshat", 99.0));
        arr.add(new Student(3, "Parul", 87.0));
        arr.add(new Student(4, "Tarun", 78.0));
        arr.add(new Student(5, "Swati", 90.0));
 
        // Iteratig over the above ArrayList
        for (int i = 0; i < arr.size(); i++)
 
            // Print the ArrayList elements as
            // in order added
            System.out.println(arr.get(i));
 
        // Calling the Collection.sort() method
        // to sort elements od ArrayList
        Collections.sort(arr);
 
        // Printing the ArrayList after sorting
        System.out.println("\nAfter Sorting :\n");
        for (int i = 0; i < arr.size(); i++)
            System.out.println(arr.get(i));
    }
}
输出
Student{RollNo=1, Name='Ankush', Marks=98.0}
Student{RollNo=2, Name='Akshat', Marks=99.0}
Student{RollNo=3, Name='Parul', Marks=87.0}
Student{RollNo=4, Name='Tarun', Marks=78.0}
Student{RollNo=5, Name='Swati', Marks=90.0}

After Sorting :

Student{RollNo=4, Name='Tarun', Marks=78.0}
Student{RollNo=3, Name='Parul', Marks=87.0}
Student{RollNo=5, Name='Swati', Marks=90.0}
Student{RollNo=1, Name='Ankush', Marks=98.0}
Student{RollNo=2, Name='Akshat', Marks=99.0}