예를 들어
import java.util.Date;
public class person {
Date birthDt;
int age;
String name;
String address_country;
String address_city;
String address_detail;
String hobby;
char gender;
double weight;
double height;
public Date getBirthDt() {
return birthDt;
}
public void setBirthDt(Date birthDt) {
this.birthDt = birthDt;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress_country() {
return address_country;
}
public void setAddress_country(String address_country) {
this.address_country = address_country;
}
public String getAddress_city() {
return address_city;
}
public void setAddress_city(String address_city) {
this.address_city = address_city;
}
public String getAddress_detail() {
return address_detail;
}
public void setAddress_detail(String address_detail) {
this.address_detail = address_detail;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
}
person class가 있고,
import java.util.Comparator;
public class PeopleCompareUtil implements Comparator<Object> {
/**
* 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.<p>
* <p>
* The implementor must ensure that {@link Integer#signum
* signum}{@code (compare(x, y)) == -signum(compare(y, x))} for
* all {@code x} and {@code y}. (This implies that {@code
* compare(x, y)} must throw an exception if and only if {@code
* compare(y, x)} throws an exception.)<p>
* <p>
* The implementor must also ensure that the relation is transitive:
* {@code ((compare(x, y)>0) && (compare(y, z)>0))} implies
* {@code compare(x, z)>0}.<p>
* <p>
* Finally, the implementor must ensure that {@code compare(x,
* y)==0} implies that {@code signum(compare(x,
* z))==signum(compare(y, z))} for all {@code z}.
*
* @param o1 the first object to be compared.
* @param o2 the second object to be compared.
* @return a negative integer, zero, or a positive integer as the
* first argument is less than, equal to, or greater than the
* second.
* @throws NullPointerException if an argument is null and this
* comparator does not permit null arguments
* @throws ClassCastException if the arguments' types prevent them from
* being compared by this comparator.
* @apiNote It is generally the case, but <i>not</i> strictly required that
* {@code (compare(x, y)==0) == (x.equals(y))}. Generally speaking,
* any comparator that violates this condition should clearly indicate
* this fact. The recommended language is "Note: this comparator
* imposes orderings that are inconsistent with equals."
*/
@Override
public int compare(Object o1, Object o2) {
Person obj1 = (Person)o1;
Person obj2 = (Person)o2;
int result = 0;
if(obj1.getAge() > obj2.getAge()) {
result = 1;
} else {
result = -1;
}
if(obj1.getAge() == obj2.getAge()) {
if(obj1.getHeight() > obj2.getHeight()){
result = 1;
}
}
return result;
}
}
이런 경우에
height 가 같은 경우에는 다루고 있지 않아서 에러를 내는 줄 알았는데..
Person에 age와 height 만 빼서 돌린 경우에는 에러안난다..
뭘까
'정보 > Language' 카테고리의 다른 글
[Javascript] Object, Array (0) | 2024.01.09 |
---|---|
Kotlin: Functions (0) | 2023.08.02 |
Kotlin: Control Flow (0) | 2023.07.31 |
Kotlin : Hello, world! (0) | 2023.07.20 |
Enum을 사용한 메뉴관리 (0) | 2022.08.26 |