Java.lang there is an interface below: comparable (comparable)

Source: Internet
Author: User
Tags comparable

For custom objects, sort does not know the rules, so it cannot be compared. In this case, you must define the collation. There are two types of methods:

Java.lang there is an interface below: comparable (comparable)

You can have a custom object implement an interface that has only one method Comparableto (object o)

The rule is that the current object is compared to an O object, which returns an int value that the system sorts according to this value.

return value >0; (can define the return value as 1), as the current object >o object

If the current object =o object, the return value = 0;

Returns the value 〈0, such as the current object <o object. (The return value can be defined as-1)

Look at the Java code of Testarraylist.

We achieve ascending and descending permutations by returning values of 1 and 1 positions.

1  Packagetomtexts;2 3  4     Abstract classFatherclass5     {6         Abstract voidAbstractmethod ();7         voidPrintmethod ()8         {9System.out.println ("Fatherclass function!"); Ten         } One     } A     classChildClassextendsFatherclass -     { -         voidAbstractmethod () the         { -System.out.println ("ChildClass function!"); -         } -     } +      Public classtomtexts_07 -     { +      Public Static voidMain (String args[]) A     { atChildClass c=NewChildClass (); - C.abstractmethod (); - C.printmethod (); -  -     }    -}

Java.lang there is an interface below: comparable (comparable)

Related Article

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.