- 객체 정렬에 필요한 메서드(정렬기준 제공)를 정의한 인터페이스
Comparable 기본 정렬기준을 구현하는데 사용. (기본 정렬기준 제공)
Comparator 기본 정렬기준 외에 다른 기준으로 정렬하고자할 때 사용
- compare()와 compareTo()는 두 객체의 비교결과를 반환하도록 작성
++
Collections.sort()를 사용할때는 반드시 Comparable또는 Comparator중 하나를 구현해줘야한다.
Java API 문서에도 Collections.sort()를 사용할 때 Comparable 또는 Comparator 중 하나가 필요하다는 내용이 명시되어 있다.
public interface Comparable<T> This interface imposes a **total ordering** on the objects of each class that implements it. This ordering is referred to as the class's **natural ordering**, and the class's `compareTo` method is referred to as its **natural comparison method**.
'Programming > 자바(Java)' 카테고리의 다른 글
11-39 TreeSet - 범위 탐색, 정렬 (0) | 2024.10.14 |
---|---|
11-34 HashSet - 순서 x, 중복 x (0) | 2024.10.14 |
11-25 Arrays (0) | 2024.10.14 |
11-22 Iterator, ListIterator, Enumeration (5) | 2024.10.14 |
11- 15 Stack & Queue (1) | 2024.10.14 |