Comparable和Comparator的用法与区别

一 应用场景:

当对自己定义的数据结构需要进行指定排序的时候,java中给了我们两个接口Comparable和Comparator

comparable接口定义一个方法:

 public interface Comparable<T> {
    public int compareTo(T o);
 }

comparator接口定义方法(jdk版本不同数量不同)注意:有些类实现了comparator,但是没有实现equals方式是因为Object类已经实现了equals方法

public interface Comparator<T> {
    int compare(T o1, T o2);
    boolean equals(Object obj);
}

区别在于:实现了comparable的对象直接就可以成为一个可以比较的对象,不过得在类中进行方法定义;comparator在对象外比较,不修改实体类。

二  实例演示

1  Comparable用法

1 定义一个Person实体类实现Comparable接口

public class Person implements Comparable<Person>{
	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<Person> list=new ArrayList<Person>();
		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<emp> list=new ArrayList<emp>();
		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<emp>(){
			@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=李四]

猜你喜欢

转载自blog.csdn.net/qq_40693828/article/details/81409004
今日推荐