Comparator interface in Java

Source: Internet
Author: User

Comparator is located under the Java.util bag

Comparator<t>

A comparison function that forces the overall ordering of an object collection. You can pass Comparator to the sort method, such as Collections.sort or Arrays.sort , allowing for precise control over the sort order. You can also use Comparator to control the order of certain data structures, such as 有序 set or 有序映射 , or to provide sorting for objects that are not 自然顺序 collection.

C.compare (E1, E2) ==0 and e1.equals (E2) when and only if for each E1 and E2 in a set of elements S When there is an equal Boolean value, Comparator C forces the ordering of S to be a sort consistent with equals .

You should be cautious when ordering ordered sets (or ordered mappings) with Comparator that have a brute force ordering capability that is inconsistent with equals. It is assumed that an ordered set (or an ordered map) with explicit Comparator C is used with the element (or key) extracted from set S . If C forces the ordering of S to be inconsistent with equals, then the ordered set (or ordered mapping) will be the behavior "weird". In particular, ordered set (or ordered mapping) will violate the general contract of set (or mapping) defined by equals .

For example, assuming that you use Comparator to c satisfy (a.equals(b) && c.compare(a, b) != 0) two elements a and b add to an empty TreeSet , the second add operation will return True (the size of the tree set will increase) because from the point of view of the tree set , a and b are unequal, even if this is Set.add contrary to the specification of the method.

Note: It is generally a good idea to let Comparator also implement java.io.Serializable , as they can be used as sorting methods in serializable data structures (like TreeSet , TreeMap ). In order to successfully serialize the data structure, Comparator (if provided) must implement Serializable.

In arithmetic, the relationship of defining a given Comparator C to the set S of a given object is to impose a forced order :

       {(x, y) such that c.compare (x, y) <= 0}.

This overall sort of quotient (quotient) is:

       

It follows the compare agreement directly, the quotient is the equivalence relation on s , and the forced sort is the whole sort on s . When we say that C forces the ordering of S to be consistent with equals , it means that the ordering quotient is the equals(Object) equivalent relationship defined by the method of the object:

This interface is a member of the Java collections Framework.

Start from the following versions:
1.2
See also:
Comparable, Serializable
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.