java.util.Collections.reverseOrder()方法是java.util.Collections類的方法。
// Returns a comparator that imposes the reverse of // the natural ordering on a collection of objects // that implement the Comparable interface. // The natural ordering is the ordering imposed by // the objects' own compareTo method public static Comparator reverseOrder()
我們可以由Collections.reverseOrder()返回的比較器以降序對列表進行排序。
// Java program to demonstrate working of Collections.reveseOrder()
// to sort a list in descending order
import java.util.*;
public class Collectionsorting
{
public static void main(String[] args)
{
// Create a list of Integers
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(30);
al.add(20);
al.add(10);
al.add(40);
al.add(50);
/* Collections.sort method is sorting the
elements of ArrayList in descending order. */
Collections.sort(al, Collections.reverseOrder());
// Let us print the sorted list
System.out.println("List after the use of Collection.reverseOrder()"+
" and Collections.sort():\n" + al);
}
}
輸出:
List after the use of Collection.reverseOrder() and Collections.sort(): [50, 40, 30, 20, 10]
我們可以使用這種方法Java - Arrays.sort()用法及代碼示例也。
// Java program to demonstrate working of Collections.reveseOrder()
// to sort an array in descending order
import java.util.*;
public class Collectionsorting
{
public static void main(String[] args)
{
// Create 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. */
Arrays.sort(arr, Collections.reverseOrder());
// Let us print the sorted array
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]
它返回一個Comparator,它對傳遞的Comparator對象施加相反的順序。我們可以使用此方法以與用戶定義的比較器相反的順序對列表進行排序。例如,在下麵的程序中,我們創建了一個反向的用戶定義比較器,以按卷編號的降序對學生進行排序。
// 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.
import java.util.*;
import java.lang.*;
import java.io.*;
// A class to represent a student.
class Student
{
int rollno;
String name, address;
// Constructor
public Student(int rollno, String name,
String address)
{
this.rollno = rollno;
this.name = name;
this.address = address;
}
// Used to print student details in main()
public String toString()
{
return this.rollno + " " + this.name +
" " + this.address;
}
}
class Sortbyroll implements Comparator<Student>
{
// Used for sorting in ascending order of
// roll number
public int compare(Student a, Student b)
{
return a.rollno - b.rollno;
}
}
// Driver class
class Main
{
public static void main (String[] args)
{
ArrayList<Student> ar = new ArrayList<Student>();
ar.add(new Student(111, "bbbb", "london"));
ar.add(new Student(131, "aaaa", "nyc"));
ar.add(new Student(121, "cccc", "jaipur"));
System.out.println("Unsorted");
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);
System.out.println("\nSorted by rollno");
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
參考文獻:
https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#reverseOrder()
相關用法
- Java Java lang.Long.byteValue()用法及代碼示例
- Java Java lang.Long.numberOfTrailingZeros()用法及代碼示例
- Java Java.util.concurrent.RecursiveAction用法及代碼示例
- Java Java.util.concurrent.Phaser用法及代碼示例
- Java Java.util.Collections.rotate()用法及代碼示例
- Java Java lang.Long.lowestOneBit()用法及代碼示例
- Java Java.util.concurrent.RecursiveTask用法及代碼示例
- Java Java.util.function.LongPredicate用法及代碼示例
- Java Java.util.Collections.disjoint()用法及代碼示例
- Java Java.util.function.DoublePredicate用法及代碼示例
- Java Java.util.function.BiPredicate用法及代碼示例
注:本文由純淨天空篩選整理自 Collections.reverseOrder() in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。