📝Comparable
- 인터페이스로 compareTo(T o) 메소드를 구현해야함
 
- 매개변수가 하나로 자기 자신과 매개변수 객체를 비교
 
- lang패키지에 있기 때문에 import 를 해줄 필요가 없음
 
public class Test {
	public static void main(String[] args)  {
 
		Student a = new Student(17, 2);	
		Student b = new Student(18, 1);	
		
		
		int isBig = a.compareTo(b);	
		
		if(isBig > 0) {
			System.out.println("a객체가 b객체보다 큽니다.");
		}
		else if(isBig == 0) {
			System.out.println("두 객체의 크기가 같습니다.");
		}
		else {
			System.out.println("a객체가 b객체보다 작습니다.");
		}
		
	}
 
}
 
class Student implements Comparable<Student> {
 
	int age;			
	int classNumber;	
	
	Student(int age, int classNumber) {
		this.age = age;
		this.classNumber = classNumber;
	}
	
	@Override
	public int compareTo(Student o) {
		return this.age - o.age;
	}
}
📝Comparator
- 인터페이스로  compare(T o1, T o2)를 구현해야함(이것 말고도 다른 메소드가 많지만 꼭 구현할 필욘 X)
 
- 매개변수가 두개로 두 매개변수 객체를 비교
 
- util패키지에 있어 import 해줘야함
 
- 익명개체 활용하는 것이 효율적
 
import java.util.Comparator;
 
public class Test {
	public static void main(String[] args)  {
 
		Student a = new Student(17, 2);	
		Student b = new Student(18, 1);	
		Student c = new Student(15, 3);	
			
		
		int isBig = comp.compare(b, c);
		
		if(isBig > 0) {
			System.out.println("b객체가 c객체보다 큽니다.");
		}
		else if(isBig == 0) {
			System.out.println("두 객체의 크기가 같습니다.");
		}
		else {
			System.out.println("b객체가 c객체보다 작습니다.");
		}
		
	}
	
	public static Comparator<Student> comp = new Comparator<Student>() {
		@Override
		public int compare(Student o1, Student o2) {
			return o1.classNumber - o2.classNumber;
		}
	};
}
 
class Student {
 
	int age;			
	int classNumber;	
	
	Student(int age, int classNumber) {
		this.age = age;
		this.classNumber = classNumber;
	}
	
}