使用 Comparator 將 Java Vector 按降序排序


Vector 實現 List 介面,用於建立動態陣列。大小不固定且可以根據需要增長的陣列稱為動態陣列。Comparator 是 ‘java.util’ 包中提供的介面。

排序是指將給定列表或陣列中的元素按升序或降序重新排列。在本文中,我們將建立一個 Vector,然後嘗試使用 Comparator 將其元素按降序排序。

使用 Java 將 Vector 按降序排序的程式

Comparator

顧名思義,它用於比較某些東西。在 Java 中,Comparator 是一個介面,用於對自定義物件進行排序。我們可以在其內建的名為 ‘compare()’ 的方法中編寫我們自己的邏輯來對指定的物件進行排序。此方法接受兩個物件作為引數,然後返回一個整數值。透過這個整數值,Comparator 決定哪個物件更大。

語法

Comparator< TypeOfComparator > nameOfComparator = new Comparator< TypeOfComparator >() {
   compare( type object1, type object1 ) {
      // logic for comparison
   }
};

nameOfComparator 傳遞給排序操作的方法,例如 ‘Collection.sort()’。

Collections.sort() 方法

Collection 介面的 ‘Collections’ 類提供了一個名為 ‘Collections.sort()’ 的靜態方法,該方法可以對指定集合(如 ArrayList 或 LinkedList)的元素進行排序。它在 ‘java.util’ 包中可用。

語法

Collections.sort( nameOfcollection, ComparatorObject );

Collections.reverseOrder()

它以相反的順序返回比較器。

示例 1

在以下示例中,我們將定義一個名為 ‘vectlist’ 的 Vector,並使用 ‘add()’ 方法在其中儲存一些物件。然後,使用 Comparator 物件和 ‘Collection.sort()’ 方法將 Vector 按降序排序。

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 

示例 2

在此示例中,首先,我們將建立一個 Comparator,並在其中定義 ‘compare()’ 方法中的邏輯以按降序對 Vector 物件進行排序。這裡的邏輯表明同時獲取兩個物件,並使用 if-else 塊進行比較。如果第一個物件大於第二個物件,則返回 -1,否則返回 1。然後,我們將比較器的物件傳遞給 ‘Collection.sort()’ 以進行排序操作。

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 

結論

本文介紹了 Comparator 介面的實現,並且我們還了解了一些內建方法的使用,例如 ‘compareTo()’、‘Collection.sort()’ 和 ‘Collections.reverseOrder()’。

更新於: 2023年5月15日

399 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告

© . All rights reserved.