Comparable接口

public interface Comparable<T>

当需要对一个对象进行排序时,该对象应该实现Comparable接口,并实现其唯一的方法:

int compareTo(T o)

在该方法中定义自己的排序规则,当调用Arrays.sort(Object[] a)方法时则回调compareTo()方法,并按照自己的规则对对象数组进行排序。

返回值及比较规则:

1、返回负值---->小于

2、返回零------>等于

3、返回正值---->大于


例子:


package conlection.list;
import java.util.Iterator;
import java.util.TreeSet;

public class Product implements Comparable<Product> {
	
	private   Integer pid;
	private   String  name;
	private   float   price;
	public Product() {
		super();
	}
	
	public Product(Integer pid, String name, float price) {
		super();
		this.pid = pid;
		this.name = name;
		this.price = price;
	}

	public Integer getPid() {
		return pid;
	}
	public void setPid(Integer pid) {
		this.pid = pid;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public float getPrice() {
		return price;
	}
	public void setPrice(float price) {
		this.price = price;
	}
	
    public String toString () {
        return (pid + "\t" + name+"\t"+price);
}

	@Override
	public int compareTo(Product arg) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		 if (pid > arg.pid)
            return 1;
    else if (pid == arg.pid)
            return 0;
    else
            return -1;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		TreeSet<Product> tset = new TreeSet<Product>();
        tset.add(new Product(111,"牙膏",14));
        tset.add(new Product(112,"篮球",14));
        tset.add(new Product(113,"足球",14));
        tset.add(new Product(7,"足球",14));

        Iterator<Product> itor = tset.iterator();
        while (itor.hasNext()) {
                System.out.println(itor.next().toString());
        }
}

 


}

Comparable接口_第1张图片






例子2:

  //定义一个类,并实现comparable接口  
    class Worker implements Comparable<Worker>{  
        private String name;  
        private int age;  
        private int salary;  
          
        public Worker(String name, int age, int salary) {  
            super();  
            this.name = name;  
            this.age = age;  
            this.salary = salary;  
        }     
      
        public String getName() {  
            return name;  
        }  
          
        public int getAge() {  
            return age;  
        }  
      
        public int getSalary() {  
            return salary;  
        }  
      
        /** 
         *实现Comparable接口的compareTo方法,在此方法中定义自己的比较规则 
         * 首先按工资有由低到高排序,过工资相同则按年龄由高到底排序 
         */  
        @Override  
        public int compareTo(Worker other) {  
            // TODO Auto-generated method stub  
            if(this.salary < other.getSalary()){     //工资小于其他人时返回负值  
                return -1;  
            }else if(this.salary > other.getSalary()){   //工资大于其他人时返回正值  
                return 1;  
            }else{                                      //工资等于其他人时按照年龄再排序  
                if(this.age < other.getAge()){  
                    return 1;//表示由高到低排序  
                }else if(this.age >= other.getAge()){  
                    return -1;  
                }  
            }  
            return 0;         
        }  
          
        @Override  
        public String toString() {  
            // TODO Auto-generated method stub  
            return this.name +"\t" + this.age + "\t" + this.salary;  
        }  
          
    };  


测试:

    public class ComparableDemo {  
      
        /** 
         * @param args 
         */  
        public static void main(String[] args) {  
            // TODO Auto-generated method stub  
              
            Worker [] workers = new Worker[5];  
            workers[0] = new Worker("test1",25,2000);  
            workers[1] = new Worker("test2",24,2100);  
            workers[2] = new Worker("test3",26,2100);  
            workers[3] = new Worker("test4",27,2200);  
            workers[4] = new Worker("test5",28,1900);  
              
            //调用Arrays.sort()方法进行排序  
            Arrays.sort(workers);  
            for(Worker w : workers){  
                System.out.println(w);  
            }  
      
        }  
      
    }  











你可能感兴趣的:(comparable)