Java 教程

Java 控制語句

面向物件程式設計

Java 內建類

Java 檔案處理

Java 錯誤與異常

Java 多執行緒

Java 同步

Java 網路程式設計

Java 集合

Java 介面

Java 資料結構

Java 集合演算法

高階 Java

Java 雜項

Java API 與框架

Java 類引用

Java 有用資源

Java - 如何使用Comparable介面?



Java Comparable 介面

Comparable 介面是一個非常重要的介面,可以被 Java 集合 用於比較自定義 物件 並對其進行排序。使用 Comparable 介面,我們可以像對 包裝類字串 物件使用集合排序方法一樣對自定義物件進行排序。

使用 Comparable 介面,我們可以使元素可排序。

Comparable 介面方法

Comparable 介面定義了一個方法:compareTo()。compareTo() 方法如下所示,用於比較傳入的物件的順序:

compare() 方法

int compareTo(Object obj)

obj 是要比較的物件。如果物件相等,則此方法返回零。如果當前物件大於 obj,則返回正值。否則,返回負值。

透過重寫 compareTo(),您可以更改物件的排序方式。例如,要以相反的順序排序,您可以實現一個反轉比較結果的比較方法。

equals() 方法

equals() 方法如下所示,測試一個物件是否等於呼叫比較器的物件:

boolean equals(Object obj)

obj 是要測試是否相等的物件。如果 obj 和呼叫物件都是 Comparator 物件並使用相同的排序順序,則此方法返回 true。否則,返回 false。

沒有必要重寫 equals(),大多數簡單的比較器都不會這樣做。

使用 Comparable 介面對自定義物件排序

在此示例中,我們使用 Comparable 介面根據比較條件對自定義物件 Dog 進行排序。

示例

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

class Dog implements Comparable<Dog> {
   private String name;
   private int age;
   Dog() {
   }

   Dog(String n, int a) {
      name = n;
      age = a;
   }

   public String getDogName() {
      return name;
   }

   public int getDogAge() {
      return age;
   }

   // Overriding the compareTo method
   public int compareTo(Dog d) {
      // compare the name using alphabetical order   
      return (this.name).compareTo(d.name);
   }

   @Override
   public String toString() {
      return this.name + "," + this.age;
   }
}

public class ComparableDemo {

   public static void main(String args[]) {
      // Takes a list o Dog objects
      List<Dog> list = new ArrayList<>();

      list.add(new Dog("Shaggy", 3));
      list.add(new Dog("Lacy", 2));
      list.add(new Dog("Roger", 10));
      list.add(new Dog("Tommy", 4));
      list.add(new Dog("Tammy", 1));

      Collections.sort(list);   // Sorts the array list
      System.out.println("Sorted by name:");
      // printing the sorted list of names
      System.out.print(list);
   }
}

輸出

這將產生以下結果:

Sorted by name:
[Lacy,2, Roger,10, Shaggy,3, Tammy,1, Tommy,4]

注意 - Arrays 類 的排序與集合的排序相同。

使用 Comparable 介面以相反的順序對自定義物件排序

示例

在此示例中,我們使用 Collections.reverseOrder() 方法 來反向排序 Dog 物件。

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

class Dog implements Comparable<Dog> {
   private String name;
   private int age;
   Dog() {
   }

   Dog(String n, int a) {
      name = n;
      age = a;
   }

   public String getDogName() {
      return name;
   }

   public int getDogAge() {
      return age;
   }

   // Overriding the compareTo method
   public int compareTo(Dog d) {
      return (this.name).compareTo(d.name);
   }

   @Override
   public String toString() {
      return this.name + "," + this.age;
   }
}

public class ComparableDemo {

   public static void main(String args[]) {
      // Takes a list o Dog objects
      List<Dog> list = new ArrayList<>();

      list.add(new Dog("Shaggy", 3));
      list.add(new Dog("Lacy", 2));
      list.add(new Dog("Roger", 10));
      list.add(new Dog("Tommy", 4));
      list.add(new Dog("Tammy", 1));

      Collections.sort(list, Collections.reverseOrder());   // Sorts the array list
      System.out.println("Sorted by name in reverse order:");
      // printing the sorted list of names
      System.out.print(list);
   }
}

輸出

這將產生以下結果:

Sorted by name in reverse order:
[Tommy,4, Tammy,1, Shaggy,3, Roger,10, Lacy,2]

示例

在此示例中,我們使用 Comparable 介面根據它們的年齡對 Dog 物件進行排序。

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

class Dog implements Comparable<Dog> {
   private String name;
   private int age;
   Dog() {
   }

   Dog(String n, int a) {
      name = n;
      age = a;
   }

   public String getDogName() {
      return name;
   }

   public int getDogAge() {
      return age;
   }

   // Overriding the compareTo method
   public int compareTo(Dog d) {
      return this.age - d.age;
   }

   @Override
   public String toString() {
      return this.name + "," + this.age;
   }
}

public class ComparableDemo {

   public static void main(String args[]) {
      // Takes a list o Dog objects
      List<Dog> list = new ArrayList<>();

      list.add(new Dog("Shaggy", 3));
      list.add(new Dog("Lacy", 2));
      list.add(new Dog("Roger", 10));
      list.add(new Dog("Tommy", 4));
      list.add(new Dog("Tammy", 1));

      Collections.sort(list);   // Sorts the array list
      System.out.println("Sorted by age:");
      // printing the sorted list by age
      System.out.print(list);
   }
}

輸出

這將產生以下結果:

Sorted by age:
[Tammy,1, Lacy,2, Shaggy,3, Tommy,4, Roger,10]
廣告