Comparable 과 Comparator 의 용법 과 차이
4099 단어 자바
자바 에 서 는 자신 이 정의 한 데이터 구 조 를 지정 하여 정렬 해 야 할 때 인터페이스 Comparable 과 Comparator 두 개 를 주 었 습 니 다.
comparable 인터페이스 정의 방법:
public interface Comparable {
public int compareTo(T o);
}
comparator 인터페이스 정의 방법 (jdk 버 전의 수량 이 다 름) 주의: 일부 클래스 는 comparator 를 실 현 했 지만 equals 방식 을 실현 하지 못 한 것 은 Object 클래스 가 equals 방법 을 실 현 했 기 때 문 입 니 다.
public interface Comparator {
int compare(T o1, T o2);
boolean equals(Object obj);
}
차이 점 은 comparable 의 대상 을 실현 하면 바로 비교 할 수 있 는 대상 이 될 수 있 지만 클래스 에서 방법 정 의 를 해 야 한 다 는 것 이다.comparator 는 대상 밖에서 비교 하고 실체 클래스 를 수정 하지 않 습 니 다.
둘째. 실례 시범
1 Comparable 용법
1. Person 실체 류 를 정의 하여 Comparable 인 터 페 이 스 를 실현 합 니 다.
public class Person implements Comparable{
public int age; //
public String name; //
//get、set
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 Person(int age, String name) {
super();
this.age = age;
this.name = name;
}
@Override
public String toString() {
return "Person [age=" + age + ", name=" + name + "]";
}
// Comparable compareTo
public int compareTo(Person o) {
if(this.age==o.age&&this.name==o.name){
return 0;
}else if(this.age>o.age){
System.out.println("this.age:"+this.age+"o.age:"+o.age);
return 1;
}else{
return -1;
}
}
public Person() {
super();
// TODO Auto-generated constructor stub
}
}
2 테스트 클래스 를 만들어 서 테스트 하기 (Junit 사용)
import org.junit.Test;
import junit.framework.TestCase;
public class TestCompare extends TestCase {
@Test
public void test1(){
List list=new ArrayList();
Person test1=new Person(66," ");
Person test2=new Person(29," ");
Person test3=new Person(28," ");
Person test4=new Person(20," ");
list.add(test4);
list.add(test3);
list.add(test2);
list.add(test1);
Collections.sort(list);
for(Object s:list){
System.out.println(s);
}
}
}
3. 실행 결과: 대상 이 comparable 인 터 페 이 스 를 실현 하면 자동 으로 정렬 합 니 다.
this.age:28o.age:20
this.age:29o.age:28
this.age:66o.age:29
Person [age=20, name= ]
Person [age=28, name= ]
Person [age=29, name= ]
Person [age=66, name= ]
2 Comparator 사용법
1 실체 클래스 정의
public class emp {
public int age;
public String name;
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 emp(int age, String name) {
super();
this.age = age;
this.name = name;
}
public emp() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "emp [age=" + age + ", name=" + name + "]";
}
}
2 테스트 클래스 테스트 작성:
import org.junit.Test;
import junit.framework.TestCase;
public class TestCompare extends TestCase {
@Test
public void test2(){
List list=new ArrayList();
emp test1=new emp(69," ");
emp test2=new emp(29," ");
emp test3=new emp(28," ");
emp test4=new emp(20," ");
list.add(test4);
list.add(test3);
list.add(test2);
list.add(test1);
Collections.sort(list,new Comparator(){
@Override
public int compare(emp o1, emp o2) {
if(o1.age==o2.age&&o1.name==o2.name){
return 0;
}else if(o1.age>o2.age){
return 1;
}else{
return 0;
}
}
});
for(Object s:list){
System.out.println(s);
}
}
}
3 실행 결과:
emp [age=20, name= ]
emp [age=28, name= ]
emp [age=29, name= ]
emp [age=69, name= ]
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.