ybbzbb
一步一步的走

java.lang.Comparable T 作用

1. 接口说明

因为我再查看String 类中看到 String 类实现了这个接口,于是就想了解一下这个接口的作用。
这个接口位于 java.lang 包下,是Java编程语言进行程序设计的基础类包。它的主要作用就是用来做对象的对比,对比之后可以使用Arrays.sort() 对装载这个对象的数组进行排序。

2. 举例说明

在百度的时候遇到了一些坑,里面有写的的bolg案例就是一个错误的案例。

/**
 * 定义一个Student的实体类,让它实现Comparable接口并实现compareTo()
 */
@SuppressWarnings("rawtypes")
public class Student implements Comparable{

    private int id;
    private String name;
    private int sex;
    
    public Student(int id, String name, int sex) {
        super();
        this.id = id;
        this.name = name;
        this.sex = sex;
    }

    public int getSex() {
        return sex;
    }
    public void setSex(int sex) {
        this.sex = sex;
    }
    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;
    }

    /* (non-Javadoc)
     *返回对比结果 0为相等,负整数为小于,正整数为大于 
     *通过修改返回值,可以做出升序排列和降序的方式 
     */
    @Override
    public int compareTo(Object o) {
        if(o instanceof Student) { 
            Student student = (Student)o; 
            if(student.getId() > this.id) { 
                return -1; 
            } else if (student.getId() < this.id) { 
                return 1; 
            } 
        } 
        return 0;
    }

}

import java.util.Arrays;

public class TestComparable {
    private static Student[] studentArray;

    public static void main(String[] args) {
        loadStudent();
        /*
         * Arrays.sort() 开始排序数据
         * 只有Student 实现Comparable对象再能够使用这个方法。
         * */
        Arrays.sort(studentArray);
        for (Student student : studentArray) {
            System.out.println("ID:" + student.getId() + "  NAME:" + student.getName() + "  SEX:" + student.getSex());
        }
    }

    //为studentArray 对象 填充无序的参数
    private static void loadStudent() {
        studentArray = new Student[10];
        Student student0 = new Student(2, "张二", 0);
        Student student1 = new Student(4, "张四", 1);
        Student student2 = new Student(6, "张六", 1);
        Student student3 = new Student(1, "张一", 0);
        Student student4 = new Student(3, "张三", 0);
        Student student5 = new Student(8, "张八", 1);
        Student student6 = new Student(9, "张九", 0);
        Student student7 = new Student(7, "张七", 1);
        Student student8 = new Student(0, "张零", 0);
        Student student9 = new Student(5, "张五", 0);
        studentArray[0] = student0;
        studentArray[1] = student1;
        studentArray[2] = student2;
        studentArray[3] = student3;
        studentArray[4] = student4;
        studentArray[5] = student5;
        studentArray[6] = student6;
        studentArray[7] = student7;
        studentArray[8] = student8;
        studentArray[9] = student9;
    }
}

3.回到主题,它在String中做了些什么?

    public int compareTo(String anotherString) {
        int len1 = value.length;
        int len2 = anotherString.value.length;
        int lim = Math.min(len1, len2);
        char v1[] = value;
        char v2[] = anotherString.value;

        int k = 0;
        while (k < lim) {
            char c1 = v1[k];
            char c2 = v2[k];
            if (c1 != c2) {
                return c1 - c2;
            }
            k++;
        }
        return len1 - len2;
    }

String 对它的实现是:

  • 其结果是负的整数,如果此String对象字典前面的参数字符串

  • 其结果是一个正整数,如果此String对象字典如下的参数字符串

  • 结果是零,如果两个字符串相等,CompareTo返回0时,equal(Object)方法将返回true。

posted on 2016-05-04 15:57  ybbzbb  阅读(439)  评论(0编辑  收藏  举报