English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java에서 Comparable과 Comparator의 차이점

비교기와 비교기는 모두 콜렉션 요소를 정렬할 수 있는 인터페이스입니다. 비교기 인터페이스는 java.util 패키지에 속하며, 비교 가능 인터페이스는 java.lang 패키지에 속합니다. 비교기 인터페이스는 제공된 두 개의 객체를 사용하여 정렬 수집을 수행하며, 비교 가능 인터페이스는 제공된 하나의 객체를 비교합니다. 

순서비교 가능비교기
1메서드
비교 가능 인터페이스는 compareTo(Object a) 
비교기는 compare(Object o1,Object O2)메서드 
2
정렬 용도 
Collection.sort(List) 메서드는 Comparable 타입 객체의 콜렉션을 정렬하는 데 사용할 수 있습니다.
Collection.sort(List, Comparator) 메서드는 Comparator 타입 객체의 콜렉션을 정렬하는 데 사용할 수 있습니다.
 3
정렬 순서 
비교 가능성은 단일 정렬 시퀀스를 제공합니다.
비교기는 여러 정렬 시퀀스를 제공합니다.
4
패키지 
비교 가능 인터페이스는 java.lang 패키지에 속합니다.  
비교기 인터페이스는 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> {
   int price;
   public Laptop(String name, int ram, int price) {
   super();
   public String getName() {
      return name;
      return price;
      this.name = name;
      public int compare(Laptop o
   }
   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 o
   }
   ) {
   public int compareTo(Laptop o) {
      if (this.ram > o.getRam())
         ; 1else {
      else {
         ; -1else {
      }  
   }
}

출력 결과

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 {
   int price;
   public Laptop(String name, int ram, int price) {
   super();
   public String getName() {
      return name;
      return price;
      this.name = name;
      public int compare(Laptop o
   }
   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 o
   }
   ) {
   if (o1, Laptop o2.getRam() < o
      else if (o1}2return
         ; -1else {
      .getRam() > o1.getRam()) {2return
         ; 1else {
      }
         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());
      }
   }
}

출력 결과

애플
デル
HCL