Detailed java Comparable interface

background

  We see the string through CompareTo method, known methods are used to compare strings that order, sorted according to the dictionary order. Java also have many classes CompareTo method, even ordering underlying algorithms are dependent on the composition of the comparison, and this comparison is dependent on a variety of data types or CompareTo Compare method. Java All compareTo methods are derived from a common interface that is Comparable. This interface has only one method, and that is CompareTo. All you want to have a comparison function of the class, it is recommended to implement this interface, rather than their own definition of this function, which is an object-oriented concepts (things that will have the same functionality to a common abstract class or interface), and for the multi-state also recommended to make the transformation by implementing the interface up to implement specific operations through the interface, which is also oriented programming interface requires us to do. Let's look at specific Comparable interface.

Comparable interface located: java.lang package. Implements this interface, will be forced to carry out natural order (dictionary sort)

View source

public interface Comparable<T> {
    public int compareTo(T o);
}

the compareTo ( T  O) comparing this object with the specified object. O for the object to be compared

If the object is less than, equal to or greater than the specified object, a negative integer, zero or a positive integer. The implementation classes returns, the return most three numbers 0 and -1

For example: Suppose that x.compareTo We (y) to "Comparison of x and y size." If return "negative", means "x smaller than Y"; return to "zero", it means "x is equal to Y '; Back" positive ", means" x is greater than Y. "

Look at the String and Integer in java to implement compareTo () is:

    private final char value[];//String的底层是字符数组  a.compareTo(b)

    public int compareTo(String anotherString) {
        int len1 = value.length;//获取调用该方法的字符串的长度a
        int len2 = anotherString.value.length;//获取比较字符串的长度b
        int lim = Math.min(len1, len2);//(a <= b) ? a : b;  min底层代码  这句代码是为了获取较短的字符串的长度
        char v1[] = value;  //创建两个字符数组,分别指向这两个字符串的所在
        char v2[] = anotherString.value;
        //循环比较,循环次数,是较短的字符串的长度,如果用较长的字符串的长度,那么会出现nullPointException
        int k = 0;
        while (k < lim) {
            char c1 = v1[k];
            char c2 = v2[k];
            //比较相对应索引的元素,如果元素不同则比较返回中间差距的顺序,如果相等,那么就继续循环比较
            if (c1 != c2) {
                return c1 - c2;//字符对应的Unicode码表中的数字,这也就是为什么说String是按照字典书序比较的,如a比b靠前,那么a对应的数字比b小,相减返回负数,差多少顺序,就返回多少
            }
            k++;
        }
        //如果两个字符串的长度不同,其它都相同,那么返回的就是长度的差距了
        return len1 - len2;
    }

We can see String compareTo is achieved according to the Unicode character code table to determine the corresponding number, returns the difference or gap between the length of the string in the character code table, based on the specific situation, but also different return following we take a look at the Integer compareTo ():

    //Integer的compareTo方法,底层依据的是compare方法,这个方法是Comparator接口的一个方法
    public int compareTo(Integer anotherInteger) {
        //实际上Integer的比较是通过Integer中包括的整数来比较的
        return compare(this.value, anotherInteger.value);
    }
    public static int compare(int x, int y) {//a.compateTo(b)
        //如果a比b小,那么返回-1,相等就是0,否则就是1
        return (x < y) ? -1 : ((x == y) ? 0 : 1);
    }

Java in addition to providing a comparison method interface also provides a comparator interface Comparator, the next section, we read Comparator.

Guess you like

Origin blog.csdn.net/qq_36850813/article/details/94387881