Comparator, Comparator
java.lang.Comparable ์ธํฐํ์ด์ค - ์ด ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ๋ ๊ฐ์ฒด ์ค์ค๋ก์๊ฒ ๋ถ์ฌํ๋ ํ๊ฐ์ง ๊ธฐ๋ณธ ์ ๋ ฌ ๊ท์น์ ์ค์ ํ๋ค
Comparable์ ๊ตฌํํ๊ณ ์๋ ํด๋์ค๋ค์ ๊ฐ์ ํ์ ์ ์ธ์คํด์ค๋ผ๋ฆฌ ์๋ก ๋น๊ตํ ์ ์๋ ํด๋์ค์ด๋ค. (wrapper ํด๋์ค, String, Date, Fileโฆ). ๊ธฐ๋ณธ ์ ๋ ฌ ๊ธฐ์ค์ ๊ตฌํํ๋ ๋ฐ์ ์ฌ์ฉํ๋ค.
์ธํฐํ์ด์ค ๋ด์ int compareTo(T o) ๋ฉ์๋๋ ๊ฐ์ฒด ์๊ธฐ ์์ (this)์ ๋งค๊ฐ๋ณ์ ๊ฐ์ฒด o๋ฅผ ๋น๊ตํ๋ค. ๋ ๊ฐ์ฒด๊ฐ ๊ฐ์ผ๋ฉด 0, ๋น๊ตํ๋ ๊ฐ๋ณด๋ค ์์ผ๋ฉด ์์, ํฌ๋ฉด ์์๋ฅผ ๋ฐํํ๋ค
๊ธฐ๋ณธ์ ์ผ๋ก ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌ๋๋๋ฐ, ๋ด๋ฆผ์ฐจ์ or ๋ค๋ฅธ ๊ธฐ์ค์ ๋ฐ๋ผ ์ ๋ ฌํ๊ณ ์ถ์ผ๋ฉด Comparator๋ฅผ ๊ตฌํํด์ ์ ๋ ฌ๊ธฐ์ค์ ์ ๊ณตํ ์ ์๋ค.
class Student implements Comparable<Student> {
int kor, eng, math;
public Student(int kor, int eng, int math){
this.kor = kor;
this.eng = eng;
this.math = math;
}
// base. ๊ตญ์ด ์ ์ ๊ธฐ์ค์ผ๋ก ์ค๋ฆ์ฐจ์ ๋ฐฉ์
@Override
public int compareTo(Student student) {
if(this.kor > student.kor) return 1;
else if(this.kor > student.kor) return -1;
return 0;
}
// // simple. 1๋ณด๋ค ๊ฐ๋จํ ๋ฐฉ์
@Override
public int compareTo(Student student) {
return this.kor - student.kor; // ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌํ๋ค๋ฉด student.kor - this.kor๋ฅผ ๋ฐํํ๋ฉด ๋๋ค
}
}
// ์ค์ . ๊ฐ์ฒด์ ์ฌ๋ฌ ํ๋๊ฐ์ ๋ํด์ ๊ฐ๊ฐ ๋ค๋ฅธ ์ ๋ ฌ๊ท์น์ ์ ์ฉ
class Student implements Comparable<Student> {
int kor, eng, math;
public Student(int kor, int eng, int math){
this.kor = kor;
this.eng = eng;
this.math = math;
}
@Override
public int compareTo(Student student) {
if(this.kor == student.kor) // ๊ตญ์ด ์ ์๊ฐ ์ผ์นํ๋ค๋ฉด
return this.eng - student.eng; // ์์ด ์ ์๋ฅผ ๊ธฐ์ค์ผ๋ก ์ค๋ฆ์ฐจ์ ์ ๋ ฌํฉ๋๋ค.
return this.kor - student.kor; // ๊ตญ์ด ์ ์๊ฐ ๋ค๋ฅด๋ค๋ฉด, ์ค๋ฆ์ฐจ์ ์ ๋ ฌํฉ๋๋ค.
}
};
java.util.Comparator ์ธํฐํ์ด์ค - ์ ๋ ฌ ๋์ ํด๋์ค์ ์ฝ๋๋ฅผ ์ง์ ์์ ํ์ง ๋ชปํ ๊ฒฝ์ฐ ์ฌ์ฉํ๋ค.
๊ธฐ๋ณธ ์ ๋ ฌ ๊ธฐ์ค ์ธ์ ๋ค๋ฅธ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ๊ณ ์ ํ ๋ ์ฌ์ฉ
Comparator ์ธํฐํ์ด์ค์ ๊ตฌํ์ฒด๋ฅผ Arrays.sort()๋ Collections.sort() ๊ฐ์ ์ ๋ ฌ ๋ฉ์๋์ ์ถ๊ฐ ์ธ์๋ก ๋๊ธด๋ค.
์ธํฐํ์ด์ค ๋ด์ int compare(T o1, T o2) ๋ฉ์๋๋ ๋ ๋งค๊ฐ๋ณ์ ๊ฐ์ฒด๋ฅผ ๋น๊ตํ๋ค.
Comparator๋ฅผ ์ต๋ช ํด๋์ค, ๋๋คํํ์์ผ๋ก ๋์ฒดํ ์ ์๋ค.
Stream์ผ๋ก ์ปฌ๋ ์ ์ด๋ ๋ฐฐ์ด์ ์ฝ๊ณ sorted() ๋ฉ์๋๋ก Comparator ๊ตฌํ์ฒด๋ฅผ ๋ฐ์์ ์ ๋ ฌํ ๋ค ์๋ก์ด ๋ฆฌ์คํธ๋ฅผ ์์ฑํ ์๋ ์๋ค.
// ๋ฐฉ๋ฒ2. comparator ์ธํฐํ์ด์ค ๊ตฌํ - ๋๋คํจ์๋ก ๋์ฒดํ ์ ์๋ค.
Arrays.sort(student, (a, b) -> a.kor - b.kor);
// String ๋ด์ ์ ์๋ ๋์๋ฌธ์ ๊ตฌ๋ถ ์์ด ์ฌ์ ์ ์ ๋ ฌํ๋ Comparator
Arrays.sort(strArr, String.CASE_INSENSITIVE_ORDER);
Arrays.sort(students, new Comparator<Student>() {
@Override
public int compare(Student a, Student b) { // base.
return a.kor - b.kor;
}
@Override
public int compare(Object o1, Object o2) { // Comparable ๊ตฌํํ์ง ์์ ๊ฐ์ฒด๋ฅผ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌ
if(o1 instanceof Comparable && o2 instanceof Comparable) {
Comparable c1 = (Comparable)o1;
Comparable c2 = (Comparable)o2;
return c1.compareTo(c2) * -1; // ๊ธฐ๋ณธ ์ ๋ ฌ๋ฐฉ์์ ์ญ์ผ๋ก ๋ณ๊ฒฝ
}
return -1;
}
});
// Stream์ผ๋ก ์๋ก์ด ์ ๋ ฌ๋ ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ ๋ฐฉ์
// Stream ํด๋์ค์ sorted() ๋ฉ์๋๋ Comparator ๊ฐ์ฒด๋ฅผ ์ธ์๋ก ๋ฐ์์ ์ ๋ ฌ๊ธฐ์ค์ผ๋ก ์ฌ์ฉํ๋ค.
List<Student> sortedList = list.stream().sorted((a, b) -> b.kor - a.kor).collect(Collectors.toList());
Last updated