📜  Java中的 Collections.reverseOrder() 和示例

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

Java中的 Collections.reverseOrder() 和示例

本身存在于Java.util 包中的 Collections 类的reverseOrder()方法返回一个比较器,使用此比较器我们可以以相反的顺序对 Collection 进行排序。自然排序是对象自己的 compareTo 方法强加的排序。

句法:

public static  Comparator reverseOrder()

参数:一个比较器,其排序将被返回的比较器反转(也可以为空)

返回类型:一个比较器,它对实现 Comparable 接口的对象集合施加自然顺序的逆向。

现在为了深入了解基层,我们将涵盖以下列出的不同用例:

  1. 按降序对列表进行排序
  2. 按降序对数组进行排序
  3. 当有用户定义的比较器进行反向时,按卷号的降序对学生进行排序。

案例1:按降序对列表进行排序

例子

Java
// Java Program to Demonstrate Working of reverseOrder()
// method of Collections class
// To sort a list in descending order
 
// Importing required utility classes
import java.util.*;
 
// Main class
// Collectionsorting
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating a list of integers for which we
        // create an empty ArrayList by
        // declaring object of ArrayList class
        ArrayList al = new ArrayList();
 
        // Custom input integer elements
        al.add(30);
        al.add(20);
        al.add(10);
        al.add(40);
        al.add(50);
 
        // Using sort() method of Collections class to
        // sort the elements and passing list and using
        // reverseOrder() method to sort in descending order
        Collections.sort(al, Collections.reverseOrder());
 
        // Lastly printing the descending sorted list on
        // console
        System.out.println(
            "List after the use of Collection.reverseOrder()"
            + " and Collections.sort() :\n" + al);
    }
}


Java
// Java Program to Demonstrate Working of reverseOrder()
// method of Collections class
// To Sort an Array in Descending Order
 
// Importing required utility classes
import java.util.*;
 
// Main class
// CollectionSorting
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an array to be sorted in descending
        // order
        Integer[] arr = { 30, 20, 40, 10 };
 
        // Collections.sort method is sorting the
        // elements of arr[] in descending order
        // later on Arrays.sort() is applied to sort array
        Arrays.sort(arr, Collections.reverseOrder());
 
        // Printing the sorted array on console
        System.out.println(
            "Array after the use of Collection.reverseOrder()"
            + " and Arrays.sort() :\n"
            + Arrays.toString(arr));
    }
}


Java
// Java Program to Demonstrate Working of
// reverseOrder(Comparator c)
// To sort students in descending order of roll numbers
// when there is a user defined comparator to do reverse
 
// Importing required classes
import java.io.*;
import java.lang.*;
import java.util.*;
 
// Class 1
// Helper student class
// to represent a student
class Student {
    int rollno;
    String name, address;
 
    // Constructor
    public Student(int rollno, String name, String address)
    {
 
        // This keyword refers to current instance itself
        this.rollno = rollno;
        this.name = name;
        this.address = address;
    }
 
    // Method of Student class
    // To print student details inside main() method
    public String toString()
    {
 
        return this.rollno + " " + this.name + " "
            + this.address;
    }
}
 
// Class 2
// Helper class implementing interface
class Sortbyroll implements Comparator {
 
    // Method
    // Used for sorting in ascending order of
    // roll number
    public int compare(Student a, Student b)
    {
        return a.rollno - b.rollno;
    }
}
 
// Class 3
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an empty ArrayList
        ArrayList ar = new ArrayList();
 
        // Adding custom attributes defined in Student class
        // using add() method
        ar.add(new Student(111, "bbbb", "london"));
        ar.add(new Student(131, "aaaa", "nyc"));
        ar.add(new Student(121, "cccc", "jaipur"));
 
        // Display message for better readability
        System.out.println("Unsorted");
 
        // Printing list of students
        for (int i = 0; i < ar.size(); i++)
            System.out.println(ar.get(i));
 
        // Sorting a list of students in descending order of
        // roll numbers using a Comparator
        // that is reverse of Sortbyroll()
        Comparator c
            = Collections.reverseOrder(new Sortbyroll());
        Collections.sort(ar, c);
 
        // Display message for better readability
        System.out.println("\nSorted by rollno");
 
        // Printing sorted students in descending order
        for (int i = 0; i < ar.size(); i++)
            System.out.println(ar.get(i));
    }
}


输出
List after the use of Collection.reverseOrder() and Collections.sort() :
[50, 40, 30, 20, 10]

案例 2:按降序对数组进行排序

例子

Java

// Java Program to Demonstrate Working of reverseOrder()
// method of Collections class
// To Sort an Array in Descending Order
 
// Importing required utility classes
import java.util.*;
 
// Main class
// CollectionSorting
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an array to be sorted in descending
        // order
        Integer[] arr = { 30, 20, 40, 10 };
 
        // Collections.sort method is sorting the
        // elements of arr[] in descending order
        // later on Arrays.sort() is applied to sort array
        Arrays.sort(arr, Collections.reverseOrder());
 
        // Printing the sorted array on console
        System.out.println(
            "Array after the use of Collection.reverseOrder()"
            + " and Arrays.sort() :\n"
            + Arrays.toString(arr));
    }
}
输出
Array after the use of Collection.reverseOrder() and Arrays.sort() :
[40, 30, 20, 10]

案例3:当有用户定义的比较器进行反向时,按卷号的降序对学生进行排序。

public static Comparator reverseOrder(Comparator c) 

它返回一个 Comparator,它对传递的 Comparator 对象施加相反的顺序。我们可以使用此方法以用户定义的 Comparator 的相反顺序对列表进行排序。例如,在下面的程序中,我们创建了用户定义的比较器的逆序,以按卷号的降序对学生进行排序。

例子:

Java

// Java Program to Demonstrate Working of
// reverseOrder(Comparator c)
// To sort students in descending order of roll numbers
// when there is a user defined comparator to do reverse
 
// Importing required classes
import java.io.*;
import java.lang.*;
import java.util.*;
 
// Class 1
// Helper student class
// to represent a student
class Student {
    int rollno;
    String name, address;
 
    // Constructor
    public Student(int rollno, String name, String address)
    {
 
        // This keyword refers to current instance itself
        this.rollno = rollno;
        this.name = name;
        this.address = address;
    }
 
    // Method of Student class
    // To print student details inside main() method
    public String toString()
    {
 
        return this.rollno + " " + this.name + " "
            + this.address;
    }
}
 
// Class 2
// Helper class implementing interface
class Sortbyroll implements Comparator {
 
    // Method
    // Used for sorting in ascending order of
    // roll number
    public int compare(Student a, Student b)
    {
        return a.rollno - b.rollno;
    }
}
 
// Class 3
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an empty ArrayList
        ArrayList ar = new ArrayList();
 
        // Adding custom attributes defined in Student class
        // using add() method
        ar.add(new Student(111, "bbbb", "london"));
        ar.add(new Student(131, "aaaa", "nyc"));
        ar.add(new Student(121, "cccc", "jaipur"));
 
        // Display message for better readability
        System.out.println("Unsorted");
 
        // Printing list of students
        for (int i = 0; i < ar.size(); i++)
            System.out.println(ar.get(i));
 
        // Sorting a list of students in descending order of
        // roll numbers using a Comparator
        // that is reverse of Sortbyroll()
        Comparator c
            = Collections.reverseOrder(new Sortbyroll());
        Collections.sort(ar, c);
 
        // Display message for better readability
        System.out.println("\nSorted by rollno");
 
        // Printing sorted students in descending order
        for (int i = 0; i < ar.size(); i++)
            System.out.println(ar.get(i));
    }
}

输出:

Unsorted
111 bbbb london
131 aaaa nyc
121 cccc jaipur

Sorted by rollno
131 aaaa nyc
121 cccc jaipur
111 bbbb london