Comparable interface

     如果要比较两个对象则Comparable接口很实用,虽然还有另一个接口Comparator,但我们现在只介绍接口Comparable用法。比较两个对象先要实现接口Comparable,并且实现compareTo方法,建议compareTo与equals比较规则一样。compareTo方法定义,查看API文档(文档已详细说明),实例结合java.util.Arrays.sort()方法。下面提供实例说明:
package cn.lang;

/**
 * <li>当且仅当 e1.compareTo(e2) == 0 与 e1.equals(e2) 具有相同的 boolean 值时,类 C 的自然排序才叫做与 equals 一致
 * null 不是任何类的实例,即使 e.equals(null) 返回 false,e.compareTo(null) 也将抛出 NullPointerException。</li>
 * <li>建议(虽然不是必需的)最好使自然排序与 equals 一致。这是因为在使用自然排序与 equals 不一致的元素(或键)时,
 * 没有显式比较器的有序集合(和有序映射表)行为表现“怪异”。尤其是,这样的有序集合(或有序映射表)
 * 违背了根据 equals 方法定义的集合(或映射表)的常规协定。</li>
 * <li>实际上,所有实现 Comparable 的 Java 核心类都具有与 equals 一致的自然排序。
 * java.math.BigDecimal 是个例外,它的自然排序将值相等但精确度不同的 BigDecimal 对象(比如 4.0 和 4.00)视为相等。</li>
 * @author Administrator
 * @since 2011-12-26
 *
 */
class Person implements Comparable<Person>{
	
	private String name;
	private int age ;
	private String school ;
	
	public Person(String name,int age,String school){
		this.name = name ;
		this.age = age ;
		this.school = school ;
	}
	@Override
	public int compareTo(Person t){
		try{
			if(t==null) throw new Exception("对象不能为空!");
		}catch(Exception e){
			System.out.println(this.getClass().getName()+"异常信息:"+e.getMessage());
		}
		if(this.age>t.age){
			return -1 ;
		}else if(this.age<t.age){
			return 1;
		}else{
			return 0 ;
		}
	}
	@Override
	public boolean equals(Object o){
		if(this == o) return true;
		if(!(o instanceof Person)) return false;
		Person p = (Person)o ;
		if(this.age==p.age){
			return true;
		}else{
			return false;
		}
	}
	@Override
	public String toString(){
		return "姓名:"+this.name+"、年龄:"+this.age+"、学校:"+this.school ;
	}
}
public class ComparableDemo01{
	/**
	 * 测试
	 * @param args
	 */
	public static void main(String[] args) {
		Person p1 = new Person("张三",10,"学校一");
		Person p2 = new Person("李四",15,"学校二");
		Person p3 = new Person("王五",8,"学校三");
		Person p4 = new Person("小李",6,"学校四");
		Person p5 = new Person("小二",16,"学校五");
		Person p[] = {p1,p2,p3,p4,p5};
		System.out.println("排序前:");
		for(Person person:p){
			System.out.println(person);
		}
		java.util.Arrays.sort(p);
		System.out.println("排序后:");
		for(Person person:p){
			System.out.println(person);
		}
	}
}



package com.yy.basic;

import java.util.Set;
import java.util.TreeSet;

public class Student implements Comparable<Student>{
	
	private int id ;
	
	private String name ;
	
	private String school ;
	
	public Student(int id, String name, String school){
		this.id = id ;
		this.name = name ; 
		this.school = school ;
	}
	
	@Override
	public int compareTo(Student o) {
		int result = 0;
		result = id > o.id ? 1 : (id == o.id ? 0 : -1);
		if(result==0){
			result = name.compareTo(o.name);
		}
		return result ;
	}
	
	@Override
	public boolean equals(Object obj) {
		if(this == obj){
			return true ;
		}
		if(obj instanceof Student){
			Student student = (Student) obj;
			if(this.id == student.id && this.name.equals(student.name) ){
				return true ;
			}
		}
		return false;
	}
	
	@Override
	public int hashCode() {
		return id + this.name.hashCode();
	}

	@Override
	public String toString() {
		
		return this.id + "_" + this.name + "_" + this.school;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSchool() {
		return school;
	}

	public void setSchool(String school) {
		this.school = school;
	}
	public static void main(String[] args) {
		Set<Student> set = new TreeSet<Student>();
		set.add(new Student(1,"oy","zh"));
		set.add(new Student(2,"oy","zh"));
		set.add(new Student(1,"o1","zh"));
		set.add(new Student(4,"oy","zh"));
		set.add(new Student(3,"oy","zh"));
		System.out.println(set);
	}
}

你可能感兴趣的:(java,equals,Arrays,comparable,compareTo)