Java 集合元素排序接口Comparable
什么是Comparable
public interface Comparable<T> { /** * Compares this object with the specified object for order. Returns a * negative integer, zero, or a positive integer as this object is less * than, equal to, or greater than the specified object. * * <p>The implementor must ensure <tt>sgn(x.compareTo(y)) == * -sgn(y.compareTo(x))</tt> for all <tt>x</tt> and <tt>y</tt>. (This * implies that <tt>x.compareTo(y)</tt> must throw an exception iff * <tt>y.compareTo(x)</tt> throws an exception.) * * <p>The implementor must also ensure that the relation is transitive: * <tt>(x.compareTo(y)>0 && y.compareTo(z)>0)</tt> implies * <tt>x.compareTo(z)>0</tt>. * * <p>Finally, the implementor must ensure that <tt>x.compareTo(y)==0</tt> * implies that <tt>sgn(x.compareTo(z)) == sgn(y.compareTo(z))</tt>, for * all <tt>z</tt>. * * <p>It is strongly recommended, but <i>not</i> strictly required that * <tt>(x.compareTo(y)==0) == (x.equals(y))</tt>. Generally speaking, any * class that implements the <tt>Comparable</tt> interface and violates * this condition should clearly indicate this fact. The recommended * language is "Note: this class has a natural ordering that is * inconsistent with equals." * * <p>In the foregoing description, the notation * <tt>sgn(</tt><i>expression</i><tt>)</tt> designates the mathematical * <i>signum</i> function, which is defined to return one of <tt>-1</tt>, * <tt>0</tt>, or <tt>1</tt> according to whether the value of * <i>expression</i> is negative, zero or positive. * * @param o the object to be compared. * @return a negative integer, zero, or a positive integer as this object * is less than, equal to, or greater than the specified object. * * @throws NullPointerException if the specified object is null * @throws ClassCastException if the specified object's type prevents it * from being compared to this object. */ public int compareTo(T o); }
- 是一个接口,定制排序规则
- 对实现它的每个类的对象进行整体排序,里面compateTo方法是实现排序的具体方法
- 比如TreeSet、SortedSet、Collections.sort()方法调用进行排序
- String、Integer等类默认实现了这个接口,所以可以排序
compareTo方法
- 用于比较次对象和指定对象的顺序,o为要比较的对象
- 返回int类型
- 大于0,表示this大于传进来的对象o,则往后排,即升序
- 等于0,表示this等于传进来的对象o
- 小于0,表示this小于传进来的对象o
案例
根据学生的年龄进行排序
import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Set; import java.util.TreeSet; public class TestCom { public static void main(String[] args) { Set<Student> studentSet = new TreeSet<>(); studentSet.add(new Student("jack", 32)); studentSet.add(new Student("tom", 22)); studentSet.add(new Student("mary", 35)); studentSet.add(new Student("tim", 11)); studentSet.add(new Student("tony", 49)); studentSet.add(new Student("dd", 30)); System.out.println(studentSet); System.out.println("=============================="); List<Student> studentList = new ArrayList<>(); studentList.add(new Student("jack", 32)); studentList.add(new Student("tom", 22)); studentList.add(new Student("mary", 35)); studentList.add(new Student("tim", 11)); studentList.add(new Student("tony", 49)); studentList.add(new Student("dd", 30)); System.out.println(studentList); Collections.sort(studentList); System.out.println(studentList); } } class Student implements Comparable { private int age; private String name; public void setAge(int age) { this.age = age; } public int getAge() { return age; } public void setName(String name) { this.name = name; } public String getName() { return name; } public Student(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "Student{" + "age=" + age + ", name='" + name + '\'' + '}'; } @Override public int compareTo(Object o) { if (o instanceof Student) { Student student = (Student) o; return this.age - student.age; } // 返回的数是0代表两个对象相同 return 0; } }