The comparable of javable

Source: Internet
Author: User

We know that there are some reusable algorithms in the Java Collection class, such as

Collections.sort () from small to large, collections.min (), minimum collections.max () maximum,

These are very common for list operations and often require

list<string> strings = new arraylist<string> () strings.add ("AB"); Strings.add ("CC"); Strings.add ("BC"); Collections.sort (strings); String Max =collections.max (strings); String min =collections.min (strings); SYSTEM.OUT.PRINTLN (strings);//[ab, BC, CC]SYSTEM.OUT.PRINTLN (max);//ccsystem.out.println (min);//ablist< integer> integers = new arraylist<integer> () Integers.add (2); Integers.add (1); Integers.add (3); Collections.sort (integers); integer max1 =collections.max (integers); integer min1 =collections.min (integers); System.out.println (integers);//[1, 2, 3]system.out.println (MAX1);//3system.out.println (min1);//1

So the problem is that the string,integer in the example is a comparable element, what if it's a custom ADT? Or how to follow the size of the way we think, such as String here is the dictionary order, Integer is ascending, I want to change a way, how to do?

There are two ways of doing this.

1 Implement the comparable sort interface and implement the public int compareTo (Object o) method

public class Useanimals {public static void main (string[] args) {list<dog> dogs = new arraylist<dog> ();D og Dog 1 = new Dog (1, "Mary");D og dog2 = new Dog (3, "Elizabeth");D og dog3 = new Dog (2, "Anne Boleyn");d Ogs.add (dog1);d ogs.add (DOG2); Dogs.add (DOG3); Collections.sort (dogs); Dog Max =collections.max (dogs);D og min =collections.min (dogs); System.out.println (dogs);//[mary 1, Anne Boleyn 2, Elizabeth 3]system.out.println (max);//elizabeth 3system.out.println (min);//mary 1}}class Dog implements comparable<dog>  {int age; String name;p ublic Dog (int age,string name) {this.age=age;this.name=name;} @Overridepublic int compareTo (Dog o) {//TODO auto-generated method Stubreturn (this.age>o.age)? 1:-1;} @Overridepublic String toString () {//TODO auto-generated method Stubreturn name+ "" +age;}}

  

2. By passing in the parameter comparator comparator to achieve

public static void Main (string[] args) {list<dog> dogs = new arraylist<dog> ();D og dog1 = new Dog (1, "Mary");D og dog2 = new Dog (3, "Elizabeth");D og dog3 = new Dog (2, "Anne Boleyn");d Ogs.add (dog1);d ogs.add (dog2);d ogs.add (DOG3);        Comparator<dog> CIA = new  comparator<dog> () {@Override public        int compare (dog O1, dog O2) {        return o1.age-o2.age;}        ; Collections.sort (dogs,cia);D og max =collections.max (dogs);D og min =collections.min (dogs); System.out.println (dogs);//[mary 1, Anne Boleyn 2, Elizabeth 3]system.out.println (max);//elizabeth 3system.out.println (min);//mary 1}

  

Of course, it can be simply written as:

Collections.sort (Dogs, new comparator<dog> () {        @Override public        int compare (dog O1, dog O2) {        return o1.age-o2.age;        }    });

This enables you to sort the custom ADT in the way you want or the maximum minimum value of a key.

Javable comparable

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.