Java Collection Class (1) comparison of--comparable and comparator

Source: Internet
Author: User

Comparable is the sort of method implementation defined within the set;

Comparator is the sort implemented outside the collection

Comparator is located under Package Java.util , while comparable is under package Java.lang

Comparable is an interface that the object itself has already supported for self-comparison, such as String, Integer itself can do the comparison size operation, has implemented the comparable interface

Comparator is a dedicated comparator, and when the object does not support self-comparison or the self-comparison function does not meet your requirements, you can write a comparator to achieve a comparison of the size between the two objects.

Using Comparator is the strategy mode (strategy design pattern), which does not change the object itself, but uses a policy object (strategy object) to change its behavior.

For example: you want to sort integers by absolute size, integer is not required, you do not need to modify the integer class (in fact, you can not do so) to change its sorting behavior, as long as the implementation of the Comparator interface object to achieve control of its ordering is OK.

Import   java.util.*;    Public class Implements Comparator {           publicint  Compare (Object O1, Object O2) {             int v1 = Math.Abs ((Integer) O1). Intvalue ());              int v2 = Math.Abs ((Integer) O2). Intvalue ());              return v1 > v2? 1: (V1 = = v2? 0:-1);}       }    

Called, or written directly to an anonymous inner class.

Arrays.sort (integers, new Abscomparator ());

Comparator defines two methods, namely int compare (t O1, T O2) and boolean equals (Object obj), to compare two comparator is No equal, sometimes when implementing the comparator interface, does not implement the Equals method, the program does not have an error , because the class that implements the interface is also a subclass of the object class, and the object class has implemented the Equals method .

comparable only the public int compareTo (T o) method , which means that if I define a person class that implements the comparable interface, then when I instantiate the person1 of the person class,   I want to compare the size of Person1 and an existing person object Person2, and I can call it this way:Person1.comparto (Person2), which can be judged by the return value, and at this point if you define a Personcomparator (implements the comparator interface), then you can do this: Personcomparator comparator= new Personcomparator (); Comparator.compare (person1,person2);

These two different invocation methods

Java Collection Class (1) comparison of--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.