Java 集合元素排序接口Comparable
发布日期:2021-05-09 01:22:50 浏览次数:21 分类:博客文章

本文共 4026 字,大约阅读时间需要 13 分钟。

什么是Comparable

public interface Comparable
{ /** * 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. * *

The implementor must ensure sgn(x.compareTo(y)) == * -sgn(y.compareTo(x)) for all x and y. (This * implies that x.compareTo(y) must throw an exception iff * y.compareTo(x) throws an exception.) * *

The implementor must also ensure that the relation is transitive: * (x.compareTo(y)>0 && y.compareTo(z)>0) implies * x.compareTo(z)>0. * *

Finally, the implementor must ensure that x.compareTo(y)==0 * implies that sgn(x.compareTo(z)) == sgn(y.compareTo(z)), for * all z. * *

It is strongly recommended, but not strictly required that * (x.compareTo(y)==0) == (x.equals(y)). Generally speaking, any * class that implements the Comparable 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." * *

In the foregoing description, the notation * sgn(expression) designates the mathematical * signum function, which is defined to return one of -1, * 0, or 1 according to whether the value of * expression 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
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
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; }}

 

上一篇:Java Objects工具类重点方法使用
下一篇:Java集合框架之Collections

发表评论

最新留言

不错!
[***.144.177.141]2025年05月03日 02时49分52秒