본 글은 우아한 테크코스 프리코스 1주차 미션 중 공부한 내용을 기록한 것이다.
-> 우아한 테크코스 프리코스 1주차 미션 java-onboarding
-> 필자가 제출한 코드
-> 1주차 미션 회고
<로직 1>
오름차순
내림차순
(Comparable의 경우, 앞 값이 자기 자신이 된다.)
<로직 2>
<유의할 점>
로직 2의 경우, 자료형 범위를 이탈한 값이 들어올 경우에 원하는 결과가 나오지 않을 수 있다.
예를 들어 int의 경우, 자료형의 범위는 (-2^31) ~ (2^31 - 1) 이다.
해당 범위를 넘어선 값이 들어올 경우, Underflow 혹은 Overflow가 발생한다.
따라서 Underflow나 Overflow가 발생할 여지가 있는지에 대한 확인이 필요하다.
로직 1처럼 부등호를 활용하여 대소 비교 후, (1, 0, -1) 중 하나를 반환하는 것이 안전하며, 권장되는 방식이다.
java.lang 패키지에 있어 import 해 줄 필요가 없다.
자기 자신과 매개변수 객체를 비교한다.
public int compareTo(T o) 메소드를 구현해야 한다.
class ComparableExample implements Comparable<ComparableExample> {
int field1;
int field2;
...
@Override
public int compareTo(ComparableExample ce1) {
if (this.field1 > ce1.field1) {
return 1;
} else if (this.field1 < ce1.field1) {
return -1;
} else {
if (this.field2 > ce1.field2) {
return 1;
} else if (this.field2 < ce1.field2) {
return -1;
} else {
return 0;
}
}
}
...
java.util 패키지에 있어 import가 필요하다.
매개변수로 입력되는 두 개의 객체를 비교한다.
public int compare(T o1, T o2) 메소드를 구현해야 한다.
class ComparatorExample implements Comparator<ComparatorExample> {
int field1;
...
@Override
public int compare(ComparatorExample ce1, ComparatorExample ce2) {
if (ce1.field1 > ce2.field2) {
return 1;
} else if (ce1.field1 < ce2.field2) {
return -1;
} else {
return 0;
}
...
이름이 정의되지 않은 객체
구현
부모클래스 이름 = new 부모클래스(매개변수) {
구현부
};
Comparator<Type> comparator = new Comparator<Type>() {
구현부
}
Comparator는 인터페이스이다. 따라서 객체를 생성할 수 없다. 하지만 위 코드의 경우, 'comparator' 라는 이름의 객체가 생성된다.
이는 익명 객체를 통해 구현된 것으로, 내부에서 Comparator 를 상속받는 클래스를 생성하고, 객체를 만들어 comparator에 넣어준 것이다.
'구현부' 에서는 Overriding 혹은 추가 기능들을 클래스 만들듯이 넣어주면 된다.
Comparator 익명 객체를 통해 비교 연산자 객체를 만들 수 있다.
Comparator<ComparatorExample> comparator = new Comparator() {
@Override
public int compare(ComparatorExample ce1, ComparatorExample ce2) {
if (ce1.field1 > ce2.field2) {
return 1;
} else if (ce1.field < ce2.field) {
return -1;
} else {
return 0;
}
}
};
if (comparator.compare(comparatorExample1, comparatorExample2) == 1) {
System.out.println("First one is bigger than second one.");
}
미션 Problem 7에서 다음과 같이 활용되었다.
Collections.sort(sortedRecommendScore, new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Map.Entry<String, Integer> score1, Map.Entry<String, Integer> score2) {
int differenceOfTwoScores = (score1.getValue() - score2.getValue()) * -1;
if (differenceOfTwoScores == 0) {
return score1.getKey().compareTo(score2.getKey());
} else {
return differenceOfTwoScores;
}
}
});