Comparable与compareTo
Comparable
1.什么是Comparable接口
此接口强行对实现它的每个类的对象进行整体排序。此排序被称为该类的自然排序 ,类的 compareTo 方法被称为它的自然比较方法 。实现此接口的对象列表(和数组)可以通过 Collections.sort (和 Arrays.sort )进行自动排序。实现此接口的对象可以用作有序映射表中的键或有序集合中的元素,无需指定比较器。 强烈推荐(虽然不是必需的)使自然排序与 equals 一致。所谓与equals一致是指对于类 C 的每一个 e1 和 e2 来说,当且仅当 (e1.compareTo((Object)e2) == 0) 与e1.equals((Object)e2) 具有相同的布尔值时,类 C 的自然排序才叫做与 equals 一致 。
2.实现什么方法
int compareTo(T o) 比较此对象与指定对象的顺序。如果该对象小于、等于或大于指定对象,则分别返回负整数、零或正整数。
强烈推荐 (x.compareTo(y)==0) == (x.equals(y)) 这种做法,但不是 严格要求这样做。一般来说,任何实现 Comparable 接口和违背此条件的类都应该清楚地指出这一事实。推荐如此阐述:“注意:此类具有与 equals 不一致的自然排序。”
参数: o - 要比较的对象。 返回:
负整数、零或正整数,根据此对象是小于、等于还是大于指定对象。 抛出:
ClassCastException - 如果指定对象的类型不允许它与此对象进行比较。
在实现Comparable接口中必须重写compareTo这个方法.代码如下:
1 package Demo4; 2 3 public class Person implements Comparable<Person>{ 4 private String name; 5 private int age; 6 private char sex; 7 8 public Person(String name, int age, char sex) { 9 super(); 10 this.name = name; 11 this.age = age; 12 this.sex = sex; 13 } 14 15 /* (非 Javadoc) 16 * @see java.lang.Object#toString() 17 */ 18 @Override 19 public String toString() { 20 return "Person [name=" + name + ", age=" + age + ", sex=" + sex + "]"; 21 } 22
//当采用不同的排序方式时,重写不同的comparaTo方法.此时对应不同的数据类型 23 //public int compareTo(Person o) { 24 // return this.sex-o.sex; 25 26 //public int compareTo(Person o) { 27 // return this.name.compareTo(o.name); 28 29 public int compareTo(Person o) { 30 return this.age-o.age; 31 } 33 }
主程序如下:
1 package Demo4; 2 3 import java.util.TreeSet; 4 5 public class Test { 6 public static void main(String[] args) { 7 TreeSet<Person> treeSet = new TreeSet<Person>(); 8 Person p1 = new Person("李四",34,'男'); 9 Person p2 = new Person("张三",23,'女'); 10 Person p3 = new Person("王五",13,'不'); 11 Person p4 = new Person("小二",25,'二'); 12 treeSet.add(p1); 13 treeSet.add(p2); 14 treeSet.add(p3); 15 treeSet.add(p4); 16 17 for (Person person : treeSet) { 18 System.out.println(person); 19 } 20 } 21 }
显示结果会根据选择排序的方式,按照字典顺序排列
Comparator
Comparator与Comparable一样,都是实现某种需求的接口. 区别:Comparable只能让储存对象所在的类实现可比较性,当类中具有多种属性,并且需要根据不同的属性来排序时,Comparable是不能完成的.Comparable需要重写comparaTo方法.
Comparator能使类中的不同属性按照需求进行排序, 需要重写compare方法.
comparator实例
1 //创建新的类,具有不同的属性 2 package Demo5; 3 4 public class Person { 5 private String name; 6 private int age; 7 private char sex; 8 public Person(String name, int age, char sex) { 9 super(); 10 this.name = name; 11 this.age = age; 12 this.sex = sex; 13 } 14 public Person() { 15 super(); 16 } 17 /** 18 * @return name 19 */ 20 public String getName() { 21 return name; 22 } 23 /** 24 * @param nane 要设置的 name 25 */ 26 public void setName(String name) { 27 this.name = name; 28 } 29 /** 30 * @return age 31 */ 32 public int getAge() { 33 return age; 34 } 35 /** 36 * @param age 要设置的 age 37 */ 38 public void setAge(int age) { 39 this.age = age; 40 } 41 /** 42 * @return sex 43 */ 44 public char getSex() { 45 return sex; 46 } 47 /** 48 * @param sex 要设置的 sex 49 */ 50 public void setSex(char sex) { 51 this.sex = sex; 52 } 53 /* (非 Javadoc) 54 * @see java.lang.Object#toString() 55 */ 56 @Override 57 public String toString() { 58 return "Person [name=" + name + ", age=" + age + ", sex=" + sex + "]"; 59 }; 60 61 } 62 63 64 //创建一个比较器 65 package Demo5; 66 67 import java.util.Comparator; 68 69 public class AgeComparator implements Comparator<Person> { 70 71 @Override 72 public int compare(Person o1, Person o2) { 73 //此处将int型转换成Integer类,可利用comparaTo进行比较,不转换则用==判断 74 if ((new Integer(o1.getAge()).compareTo(new Integer(o2.getAge()))==0)) { 75 if(o1.getName().compareTo(o2.getName())==0){ 76 if(o1.getSex()==o2.getSex()){ 77 return 0 ; 78 }else{ 79 return o1.getSex()-o2.getSex(); 80 } 81 82 }else{ 83 return o1.getName().compareTo(o2.getName()); 84 } 85 86 } 87 return o1.getAge() - o2.getAge(); 88 89 } 90 } 91 92 //主函数 93 package Demo5; 94 95 import java.util.TreeSet; 96 97 public class Test { 98 public static void main(String[] args) { 99 100 // 1 NameComparator namecomparator = new NameComparator(); 101 // TreeSet<Person> set = new TreeSet<Person>(new NameComparator()); 102 // 需要利用哪个比较器就选择哪个,匿名内部类可以简化代码 103 TreeSet<Person> set = new TreeSet<Person>(new AgeComparator()); 104 Person p1 = new Person("小红",23,'不'); 105 Person p2 = new Person("小明",19,'女'); 106 Person p3 = new Person("小花",16,'男'); 107 Person p4 = new Person("现场看",14,'女'); 108 Person p5 = new Person("小黑",24,'非'); 109 set.add(p1); 110 set.add(p2); 111 set.add(p3); 112 set.add(p4); 113 set.add(p5); 114 115 for (Person person : set) { 116 System.out.println(person); 117 } 118 } 119 }