본문 바로가기

TIL

22.12.26 TIL

오늘 한 일

  • 그래프 기본탐색 알고리즘, 탐욕알고리즘 듣기
  • 프로그래머스 알고리즘 1단계 2문제 풀기
  • 배열, 스택, 큐 안보고 구현해보기

공부하면서 궁금한 점

그리디 알고리즘

-매 순간 최적이라고 생각 되는 경우를 선택하는 방식으로 값을 구하는 알고리즘

 

Comparable과 Comparator

override를 통해 객체를 비교할 수 있도록 만드는 인터페이스다

 

[Comparable]

docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html#method.summary

 

Comparable (Java Platform SE 8 )

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. Lists (and arrays) of o

docs.oracle.com

 

[Comparator]

docs.oracle.com/javase/8/docs/api/java/util/Comparator.html#method.summary

 

Comparator (Java Platform SE 8 )

Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. In the foregoing description, the notation sgn(expression) designates the mathematical s

docs.oracle.com

 

 

Comparable은 implements를 해서 compareTo(T o) 메서드를 override 해주어야하고

Comparator은 일반적으로 별도의 클래스를 정의해서 compare(T o1, T o2) 메서드를 구현해준다

 

그렇다면, 둘다 정렬기준을 구현하기 위해 사용하는데 두 인터페이스의 차이가 뭘까?

 

메서드의 매개변수를 보고 예상할 수 있겠지만, Comparable은 자기 자신과 매개변수 객체를 

비교하는 것이고, Comparator는 두 매개변수 객체를 비교하는 것이다.

즉, 비교 대상이 다른 것이다.


 내일 할 일

  • 최단 경로 알고리즘, 트루스칼 알고리즘 듣기
  • 프로그래머스 알고리즘 1단계 2문제 풀기
  • 자율 학습

'TIL' 카테고리의 다른 글

22.12.30 TIL  (0) 2022.12.30
22.12.29 TIL  (0) 2022.12.30
22.12.27 TIL  (0) 2022.12.27
22.12.23 TIL  (0) 2022.12.23
22.12.22 TIL  (0) 2022.12.22