Java 中的 Comparable 與 Comparator 的區別


Comparable 和 comparator 都是用於對集合元素排序的介面。Comparator 介面屬於 java.util 包,而 comparable 屬於 java.lang 包。Comparator 介面使用提供給它的兩個物件對集合進行排序,而 comparable 介面比較“this”指代給它的一個物件。

序號關鍵ComparableComparator
1方法
Comparable 介面有一個 compareTo(Object a ) 方法
Comparator 介面有一個 compare(Object o1, Object O2) 方法
2
排序用途
Collection.sort(List) 方法可以用來對 Comparable 型別物件進行排序。
Collection.sort(List, Comparator) 方法可以用來對 Comparator 型別物件進行排序。
3
排序序列
Comparable 提供單一排序序列。
Comparator 提供多個排序序列。
4

Comparable 介面屬於 java.lang 包。
Comparator 介面屬於 java.util 包。

Comparable 示例

public class ComparableExample {
   public static void main(String[] args) {
      List<Laptop> laptopList = new ArrayList<>();
      laptopList.add(new Laptop("HCL", 16, 800));
      laptopList.add(new Laptop("Apple", 8, 100));
      laptopList.add(new Laptop("Dell", 4, 600));
      Collections.sort(laptopList);
      for (Laptop lap : laptopList) {
         System.out.println(lap.getRam());
      }
   }
}
public class Laptop implements Comparable<Laptop> {
   String name;
   int ram;
   int price;
   public Laptop(String name, int ram, int price) {
      super();
      this.name = name;
      this.ram = ram;
      this.price = price;
   }
   public String getName() {
      return name;
   }
   public int getRam() {
      return ram;
   }
   public void setRam(int ram) {
      this.ram = ram;
   }
   public void setName(String name) {
      this.name = name;
   }
   public int getPrice() {
      return price;
   }
   public void setPrice(int price) {
      this.price = price;
   }
   @Override
   public int compareTo(Laptop o) {
      if (this.ram > o.getRam())
         return 1;
      else {
         return -1;
      }  
   }
}

輸出

4
8
16

Comparator 示例

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

public class Laptop implements Comparator {
   String name;
   int ram;
   int price;
   public Laptop(String name, int ram, int price) {
      super();
      this.name = name;
      this.ram = ram;
      this.price = price;
   }
   public String getName() {
      return name;
   }
   public int getRam() {
      return ram;
   }
   public void setRam(int ram) {
      this.ram = ram;
   }
   public void setName(String name) {
      this.name = name;
   }
   public int getPrice() {
      return price;
   }
   public void setPrice(int price) {
      this.price = price;
   }
   @Override
   public int compare(Laptop o1, Laptop o2) {
      if (o1.getRam() < o2.getRam()) {
         return -1;
      }else if (o1.getRam() > o2.getRam()) {
         return 1;
      } else {
         return 0;
      }
   }
   public static void main(String[] args) {
      List laptopList = new ArrayList<>();
      laptopList.add(new Laptop("HCL", 16, 800));
      laptopList.add(new Laptop("Apple", 8, 100));
      laptopList.add(new Laptop("Dell", 4, 600));
      Comparator com = (Laptop o1, Laptop o2) -> o1.getName().compareTo(o2.getName());
      Collections.sort(laptopList, com);
      for (Laptop lap : laptopList) {
         System.out.println(lap.getName());
      }
   }
}

輸出

Apple
Dell
HCL

更新日期:08-Jul-2020

3K+ 瀏覽量

開啟你的職業生涯

透過完成課程來獲得認證

開始
廣告