Java comparable and comparator

Source: Internet
Author: User

In the sort of Java arrays, collection, and maps, the comparable and comparator interfaces are often used.

1, comparable

We can sort the array by the Arrays.sort () method:

     Public Static void Main (string[] args) {        int[] intarr={1,5,7,6,3,4};        Arrays.sort (intarr);        System.out.println (arrays.tostring (intarr));         /*         Output:            [1, 3, 4, 5, 6, 7]        */    }

Arrays is by transforming the objects in the array into comparable, and then comparing the size by the CompareTo method,

If it is our custom type array, we need to implement the comparable interface to sort by this method.

2, Comparator

Comparable's limitations are also obvious, when we need another rule to sort or reverse order, through comparable can not be implemented, but through the comparator interface, it is easy to solve this problem.

Compared to comparable is implemented within the class, the comparator is implemented externally and has no effect on the original class, so it is much more flexible.

For example, in TreeMap, we can create comparator on demand to determine how to sort:

 Public classTest { Public Static voidMain (string[] args) {Comparator<Person> Comparator =NewComparator<person>() {@Override Public intCompare (person O1, person O2) {returnO1.getid ()-O2.getid ();        }        }; Map<person, string> map =NewTreemap<>(Comparator); Map.put (NewPerson (1), "first"); Map.put (NewPerson (2), "second"); Map.put (NewPerson (4), "third"); Map.put (NewPerson (3), "fourth");        Iterate (map); /*output:key:1;value:first key:2;value:second key:3;value:fourth ke Y:4;value:third*/    }     Public Static<K,V>voidIterate (map<k,v>map) {         for(Entry<k, v>Entry:map.entrySet ()) {System.out.println ("Key:" +entry.getkey () + "; Value:" +Entry.getvalue ()); }    }}classperson{Private intID;  PublicPerson (intID) { This. ID =ID; }     Public voidSetId (intID) { This. ID =ID; }     Public intgetId () {returnID; } @Override PublicString toString () {//TODO auto-generated Method Stub        returnId+ ""; }}

Java comparable and comparator

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.