00.
자바의 java.util.Objects 클래스를 최상위 클래스인 java.lang.Object 클래스와 헷갈리지 말자.
01. Comparator<T> 인터페이스 구현을 통한 객체비교
- Comparator<T> 인터페이스를 구현함으로써 특정 클래스 타입의 객체들을 비교할 수 있다.
- Comparator<T> 인터페이스를 구현하는 클래스는 int compare(T a, T b); 메소드를 재정의해야 한다.
먼저 실행클래스 내부에 Person클래스를 만들어줬다.
Person 클래스의 필드는 주민번호 뒷자리, 나이, 키로 구성되어 있다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public class ComparePersonExample {
public static void main(String[] args) {
} //end main
static class Person {
int ssn;
int age;
double height;
Person(int ssn, int age, double height){
this.ssn = ssn;
this.age = age;
this.height = height;
}
} //end class Person{ }
} // end class ComparePersonExample { }
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
역시 실행클래스 내부에 PersonComparator 클래스를 만들어줬다.
이 클래스는 Comparator<T>인터페이스를 구현하고 있는데, 여기서 타입은 Person으로 지정해줬다.
위에서 얘기했다시피 Comparator<T> 인터페이스를 구현하는 클래스는 int compare(T a, T b); 메소드를 재정의해야 한다.
여기서는 주민등록번호 뒷자리가 같으면 동일인으로 인식하게끔 했다.
그 동안 배운대로라면 아래와 같은 코드가 필요하다.
if(o1.ssn == o2.ssn) { return 0; }
else return -1;
하지만 compare 메소드를 사용하면, 비교 객체가 같으면 0, 다르면 -1을 자동으로 리턴한다. (#24)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
public class ComparePersonExample {
public static void main(String[] args) {
} //end main
static class Person {
int ssn;
int age;
double height;
Person(int ssn, int age, double height){
this.ssn = ssn;
this.age = age;
this.height = height;
}
} //class Person{ }
static class PersonComparator implements Comparator<Person> {
@Override
public int compare(Person o1, Person o2) {
return Integer.compare(o1.ssn, o2.ssn);
// return compare(o1, o2); //이런건 안 됨
}
} //end class PersonComparator
}// end class ComparePersonExample
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
실행클래스의 내용까지 작성하고 비교한다.
당연히 처음에는 -1, 두번째는 0이 리턴될 것이다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
public class ComparePersonExample {
public static void main(String[] args) {
Person p1 = new Person(1234567, 23, 163.7);
Person p2 = new Person(2023456, 24, 163.7);
Person p3 = new Person(1234567, 23, 163.7);
int result = Objects.compare(p1, p2, new PersonComparator());
System.out.println(result);
result = Objects.compare(p1, p3, new PersonComparator());
System.out.println(result);
} //end main
static class Person {
int ssn;
int age;
double height;
Person(int ssn, int age, double height){
this.ssn = ssn;
this.age = age;
this.height = height;
}
} //class Person{ }
static class PersonComparator implements Comparator<Person> {
@Override
public int compare(Person o1, Person o2) {
return Integer.compare(o1.ssn, o2.ssn);
}
} //class PersonComparator
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
'IT > Java' 카테고리의 다른 글
19_비밀번호 생성기 만들기 (0) | 2020.01.11 |
---|---|
18_자바 System 클래스 : exit 메소드, 프로그램 실행 소요 시간 구하기 (0) | 2019.12.20 |
16_자바에서 객체 복제하기 : 얕은 복제와 깊은 복제 (0) | 2019.12.17 |
15_자바 예외 처리 : try-with-resources, 사용자 정의 예외 클래스 (0) | 2019.12.13 |
14_자바 예외 : 에러(Error)와 예외(Exception), 자바 예외의 종류 (0) | 2019.12.13 |