比较器Comparable和Comparator

内部比较器Comparable

【1】Comparable<T>是java.lang包下的一个接口

【2】主要方法为 int compareTo(T o),比较此对象与指定对象的顺序,如果该对象小于、等于或大于指定对象,则分别  返回负整数、零和正整数。

【3】Comparable<T>定义在 自定义类,如Person类的内部,通过public class Person implements Comparable{}来  使用,其中覆写int compareTo() 方法,并使用Collections.sort(Person的list)来排序。

【4】对Student类进行排序的实例:

package Gener;
import java.util.Iterator;  
import java.util.TreeSet; 
public class TreeSetTest {
	public static void main(String args []){
		Student stu1 = new Student("zhangsan", 97);
		Student stu2 = new Student("lisi", 44);
		Student stu3 = new Student("wangwu", 55);
		Student stu4 = new Student("zhaoliu", 67);
		
		TreeSet<Student> treeSet=new TreeSet<Student>();//建立新的TreeSet  
		//TreeSet 是基于TreeMap的,按照二叉树的方式将插入的元素放入指定的位置
        treeSet.add(stu1);  
        treeSet.add(stu2);  
        treeSet.add(stu3);  
        treeSet.add(stu4);  
		
		Iterator<Student> iter = treeSet.iterator(); 
		while(iter.hasNext()){
			Student t = iter.next();
			System.out.println(t.toString());
		}
	}
}

class Student implements Comparable{
	private String name;
	private int age;
	
	public Student(String name, int age){
		this.name = name;
		this.age = age;
	}
	
	public int getAge(){
		return this.age;
	}
	public String getName(){
		return this.name;
	}
	public String toString(){
		return"姓名:"+this.getName()+" 年龄:"+this.getAge();
	}
	
	public int compareTo(Object obj){
		Student s = (Student)obj;
		if(s.age < this.age){
			return -1;
		}
		if(s.age == this.age){
			return this.name.compareTo(s.name);
		}
		if(s.age > this.age){
			return 1;
		}
		return 0;
		
	}
} 

外部比较器Comparator

【1】Comparator<T>是java.util包下的一个接口

【2】主要方法为 int compare(T o1, T o2),比较第一个参数和第二个参数的大小,如果第一个参数小于、等于或大于  第二个参数,则分别返回负整数、零和正整数。

【3】Comparator<T>定义在 自定义类,如Person类的外部,通过public class Person implements Comparator{}来  使用,其中覆写int compare(T o1, T o2) 方法,并使用Collections.sort(Person的list)来排序。

【4】对Student类进行排序的实例:

package Gener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class TreeSetTest2 {
	public static void main(String args[]){
		List<Teacher> list = new ArrayList<Teacher>();//泛型队列,List允许有相同的元素,不是同步的
		list.add(new Teacher("zhangsan", 78));
		list.add(new Teacher("lisi", 33));
		list.add(new Teacher("wangwu", 33));
		list.add(new Teacher("zhaoliu", 94));
		
		StuComp stucom = new StuComp();//新建一个比较器
		Collections.sort(list, stucom);//进行比较
		
		for(Teacher t : list){
			System.out.println(t);
		}
	}
}

class Teacher{
	public String name;
	public int age;
	
	public Teacher(String name, int age){
		this.name = name;
		this.age = age;
	}
	
	public int getAge(){
		return this.age;
	}
	
	public String getName(){
		return this.name;
	}
	
	public String toString(){
		return "姓名:"+this.name+" 年龄:"+this.age;
	}
}

class StuComp implements Comparator<Teacher>{
	public int compare(Teacher th1, Teacher th2){
		// 实现一个降序的排列
		if(th1.getAge() < th2.getAge()){
			return 1;
		}
		else if(th1.getAge() == th2.getAge()){
			return th1.getName().compareTo(th2.getName());// 这里的compareTo是按字典顺序比较两个字符串。
		}
		else if(th1.getAge() > th2.getAge()){
			return -1;
		}
		return 0;
			
	}
			
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值