Vectors實作了List接口,用於建立動態數組。大小不固定且可以根據我們的需求成長的陣列稱為動態陣列。 Comparator是‘java.util’套件中可用的一個介面。
排序意味著按升序或降序重新排列給定列表或陣列的元素。在本文中,我們將建立一個向量,然後嘗試使用比較器按降序對其元素進行排序。
如其名稱所示,它用於比較某些東西。在Java中,Comparator是一個接口,用於對自訂物件進行排序。我們可以在其內建方法「compare()」中編寫自己的邏輯來對指定的物件進行排序。此方法接受兩個物件作為參數,然後傳回一個整數值。透過這個整數值,Comparator決定哪個物件更大。
Comparator< TypeOfComparator > nameOfComparator = new Comparator< TypeOfComparator >() { compare( type object1, type object1 ) { // logic for comparison } };
在像是‘Collection.sort()’這樣的方法中,nameOfComparator是用來排序作業的參數。
The class ‘Collections’ of the Collection Interface provides a static method named ‘Collections.sort()’ that can sort elements of specified collections like ArrayList or LinkedList. It is available in ‘java.util’ package.##il’
Collections.sort( nameOfcollection, ComparatorObject );
#It returns the comparator in reverse order.
In the following example, we will define a vector named 'vectlist' and store a few objects in it by using the 'add()' method. Then, use the Comparator object and 'Collection.sort()' method to sort the vector in descending order.
import java.util.*; public class VectClass { public static void main(String args[]) { // Creation of vector Vector<Integer> vectList = new Vector<>(); // Adding elements in the vector vectList.add(97); vectList.add(93); vectList.add(95); vectList.add(99); vectList.add(82); vectList.add(88); System.out.println("Elements of the unsorted list: "); // loop to iterate through elements for(int i = 0; i < vectList.size(); i++ ) { // to print the elements of the vector System.out.print(vectList.get(i) + " "); } System.out.println(); // Using comparator interface for sorting Comparator comp = Collections.reverseOrder(); Collections.sort(vectList, comp); System.out.println("Elements of the newly sorted list: "); // loop to iterate through elements for(int i = 0; i < vectList.size(); i++ ) { // to print the elements of the new vector System.out.print(vectList.get(i) + " "); } } }
Note: VectClass.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. Elements of the unsorted list: 97 93 95 99 82 88 Elements of the newly sorted list: 99 97 95 93 88 82
In this example, first, we will create a Comparator and inside it, we define our logic in 'compare()' method to sort the vector objects in descending order. The logic here states that take at objects in descending order。 and compare them using the if-else block. If first object is greater than second return -1 otherwise 1. Then, we pass the object of comparator to 'Collection.sort()' for sorting operation.
import java.util.*; public class VectClass { public static void main(String args[]) { // Using comparator interface for sorting Comparator<Integer> comp = new Comparator<Integer>() { // logic to sort in descending order public int compare(Integer i, Integer j) { if(i < j) { return 1; } else { return -1; } } }; // Creation of vector Vector<Integer> vectList = new Vector<>(); // Adding elements in the vector vectList.add(97); vectList.add(93); vectList.add(95); vectList.add(99); vectList.add(82); vectList.add(88); System.out.println("Elements of the unsorted list: "); // loop to iterate through elements for(int i = 0; i < vectList.size(); i++ ) { // to print the elements of the vector System.out.print(vectList.get(i) + " "); } System.out.println(); Collections.sort(vectList, comp); // sort using comparator System.out.println("Elements of the newly sorted list: "); // loop to iterate through elements for(int i = 0; i < vectList.size(); i++ ) { // to print the elements of the new vector System.out.print(vectList.get(i) + " "); } } }
Elements of the unsorted list: 97 93 95 99 82 88 Elements of the newly sorted list: 99 97 95 93 88 82
This article has explained the implementation of the Comparator Interface and also we discovered the use of a few inbuilt methods such as 'compareTo()', 'Collection.sort()' and 'Collections.reverseOrder()'.
以上是使用比較器將Java向量依降序排序的詳細內容。更多資訊請關注PHP中文網其他相關文章!