Comparable
- 인터페이스. 가지고 있는 메서드는 CompareTo()
- import 필요없다. java.lang에 위치해있기 때문.
- 이러한 비교하는 인터페이스를 가져오는건 궁극적으로 비교도 있지만 정렬의 기준을 세우는 것이다.
- return 값은 0,1,-1이얌.
class Student implements Comparable<Student>{
..... 생성자, getter setter....
public int CompareTo(Student s){
return this.id-s.id;
}
//아니 그럼 이름과 같은 문자일땐 어떻게 비교하나요?
//이런식으로 comapreTo내부에 compareTo를 써서 정렬의 기준을 잡아주면 됩니다.
public int CompareTo(Student s){
return this.name.compareTo(s.name);
}
}
//main method라 치면.
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Student_Comparable[] students ={new Student_Comparable(1,"park","a",1),
new Student_Comparable(136,"park","a",1),
new Student_Comparable(2322,"kim","e",2),
new Student_Comparable(3325,"pk","d",3),
new Student_Comparable(214,"dfrk","k",1),
new Student_Comparable(143,"phhrk","m",4),
new Student_Comparable(456,"nigk","n",1),
new Student_Comparable(2345,"pckrk","g",2),
new Student_Comparable(32452,"sark","d",2),
};
Arrays.sort(students);
for(Student_Comparable s : students){
System.out.println(s.getGrade());
}
}
}
이렇게 배열을 정렬할때의 기준이 compareTo의 리턴값으로 결정되는 것.
Comparator
- 얘는 약간 까다롭다. 얘는 따로 내부 클래스를 선언해줘서 사용해요.
- 또한 java.util에 있어서 import를 해줘야함.
- 즉 따로 내부 클래스를 만들기 때문에 전체 student class에는 implement 해줄 필요가 없어.
- Student 내부에 클래스를 만들고 그 클래스에 implements를 하고 아래 메서드에 compare()를 만들어
- 그리고 나서 student 아래에다가 아까 생성한 Comparator class의 객체를 만드는것
- public static class WithName implements…. 아래에다가 public static int compare(x1,x2)
- 그리고 위에다가 이제 compname = new WithName();
- 만든후 메인에다가 Arrays.sort(배열이름, Student.CompName) - 해주면 ㅇㅋ
import java.util.Comparator
class Student_Comparator {
public static Comparator<Student_Comparator> CompName = new Withname();
public static Comparator<Student_Comparator> CompID = new WithID();
//생략
public static class WithName implements Comparator<Student_Comparator>{
@Override
public static int compare(Student s1, Student s2){
return s1.name.compareTo(s2.name);
}
}
public static class WithID implements Comparator<Student_Comparator>{
@Override
public static int compare(Student s1, Student s2){
return s1.id-s2.id;
}
//main method
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Student_Comparator[] studentsforComparator ={new Student_Comparator(1,"park","a",1),
new Student_Comparator(136,"park","a",1),
new Student_Comparator(2322,"kim","e",2),
new Student_Comparator(3325,"pk","d",3),
new Student_Comparator(214,"dfrk","k",1),
new Student_Comparator(143,"phhrk","m",4),
new Student_Comparator(456,"nigk","n",1),
new Student_Comparator(2345,"pckrk","g",2),
new Student_Comparator(32452,"sark","d",2
),
};
Arrays.sort(students);
for(Student_Comparator s : students){
System.out.println(s.getGrade());
}
//CompName을 만든, 즉 내부클래스를 만든 그 클래스에 있는 녀석이니까요
//
Arrays.sort(students,Student.CompName);
for(Student_Comparator s : students){
System.out.println(s.getGrade());
}
}
}
'Algorithm_PS > Algorithm_Data Structure' 카테고리의 다른 글
DataStructure : Union-Find (0) | 2024.06.05 |
---|---|
About BinaryHeap (0) | 2024.05.23 |
<카운팅 정렬> (1) | 2023.11.17 |
<선택 정렬 & 삽입 정렬> (0) | 2023.11.11 |
자료구조 <배열의 속성> (0) | 2023.11.06 |