"Effective Java" reading Records-12th-consider implementing the comparable interface

Source: Internet
Author: User
Tags comparable

The CompareTo method is not declared in object and is the only method in the comparable interface. CompareTo not only allows for simple equivalence comparisons, but also allows sequential comparisons to be performed .

Once the comparable interface is implemented, it can collaborate with many generic algorithms (generic algorithm) and a collection implementation (collection implementation) that relies on that interface. In fact, the values class (value classes) in the Java Platform Class Library implements the comparable interface.

If you are writing a value class that has a distinct intrinsic ordering relationship, then you should strongly consider implementing this interface.

The common conventions of the CompareTo method and equals are similar:

Compares this object with the specified object. Returns the same negative integer, 0, or positive integer, respectively, when the object is less than, equal to, or greater than the specified object. If the object cannot be compared because of the type of the specified object, a ClassCastException exception is thrown.

CompareTo the rules to be followed .

Here is the write description, the symbolic sgn (expression) that represents the Signum function in mathematics, which returns 1, 0, or 1, depending on the value of the expression (expression) as negative, 0, and positive.

The implementing person must ensure that all x and Y are satisfied with sgn (X.compareto (y)) ==-sgn (Y.compareto (x)).

The implementing person must also ensure that the comparison is transitive: (X.compareto (y) >0 && Y.compareto (z)) implies X.compareto (z) >0.

The implementing person must ensure that X.compareto (y) ==0 implies that all z satisfies the SGN (X.compareto (z)) ==sgn (Y.compareto (z)).

It is strongly recommended (X.compareto (y) ==0) = = (X.equals (y)), but this is not absolutely necessary.

The CompareTo method is very similar to the rules of the Equals method, but there are several significant differences .

1. The comparable interface is parameterized and the comparable method is a static type, so there is no need for a type check and no type conversion of its arguments. If it is null, the NullPointerException exception should be thrown.

2. The comparison of the fields in the CompareTo method is the comparison of the order, not the equivalence.

3, if the class does not implement the comparable interface can use the displayed comparator to replace.

4, the implementation of the comparable interface, only use the following code

Implements comparable<xxxclass> the type of the parameter of the CompareTo method that is implemented this way, otherwise the object type argument is made.

5, the comparison of the basic types of integers can use the < and > relational operators, and floating-point type with double.compare or Float.compare.

6. If you have multiple key domains, you must start with the most critical domains and step into all the important domains.

The CompareTo code for the PhoneNumber of article 9th is as follows:

public int compareTo (PhoneNumber pn) {
        //compare area codes
        if (this.areacode<pn.areacode) return-1;
        if (This.areacode>pn.areacode) return 1;
        Area codes are equal, compare prefix
        if (this.prefix<pn.prefix) return-1;
        if (This.prefix>pn.prefix) return 1;
        Area codes and prefixes are equal, compare line numbers
        if (this.linenumber<pn.linenumber) return-1;
        if (this.linenumber>pn.linenumber) return 1;
        Return  0;//all fields are equal
    }
Although the above method is feasible, the code to be changed to the following method will be more concise and the efficiency may be higher.

public int compareTo (PhoneNumber pn) {
        //compare area codes
        int Areacodediff =this.areacode-pn.areacode;
        if (areacodediff!=0) return areacodediff;
        Area codes are equal, compare prefix
        int prefixdiff =this.prefix-pn.prefix;
        if (prefixdiff!=0) return prefixdiff;
        Area codes and prefixes are equal, compare line numbers return
        this.linenumber-pn.linenumber;
    }
Use of this technique must be very careful, unless you can ensure that the difference between the two values between the Integer.max_value.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.