Comparator (Java 2 Platform SE 5.0)

Source: Internet
Author: User
Tags data structures sort

public interface Comparator<T>

The comparison function forces the collection of some objects to be sorted as a whole. Comparator can be passed to the sort method, such as collections.sort, allowing precise control over the sort order. You can also use Comparator to control the order of certain data structures, such as TreeSet or TREEMAP.

When and only if, for each E1 and E2 in a set of elements S, (compare (object) e1, (object) E2) and ==0 (object) e1.equals) have an equal Boolean value, E2 C forces the Sorting is called a sort consistent with the equals sign.

Care should be taken when ordering a ordered set (or ordered map) using a comparator that has the same force-ordering capability as the equal sign. Suppose an ordered set (or ordered map) with an explicit Comparator C is used with an element (or key) that is extracted from set S. If C forces the sort of S to be consistent with the equals sign, then ordered set (or ordered mapping) would be "bizarre". In particular, ordered sets (or ordered mappings) that violate the general agreement defined by the equals set (or map).

For example, if you use comparator C to add two keys A and B of A.equals (object B) && C.compare (Object A, (object) b)!= 0 to an ordered set, the second The add operation returns False (the size of the ordered set does not increase) because from the point of view of the ordered set, A and B are equal.

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

In arithmetic, the formula for defining a given comparator c to force a whole sort on a given object set S is:

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

The whole sort of quotient is:{(x, y) such that c.compare((Object)x, (Object)y) == 0}.

It follows the compare agreement directly, quotient is the equivalence relation on S, the natural order is the whole sort on s. When we say that C forces the sort of S to be consistent with the equals sign, it means that the quotient of natural order is the equivalence relation defined by the Equals (object) method of the object:

{(x, y) such that x.equals((Object)y)}.

This interface is a member of the Java collections Framework.

Starting from the following version: 1.2 See also: Comparable, Arrays.sort (object[], Comparator), TreeMap, TreeSet, SortedMap, SortedSet, Serializable

Method Summary
Int Compare (To1, To2)
Compares the two parameters used to sort.
Boolean Equals (Objectobj)
Indicates whether the other object "equals" this Comparator.
 

Method details

Compare int compare(To1,
      To2)
compares the two parameters used to sort. Returns a negative integer, 0, or positive integer, respectively, with the first argument less than, equal to, or greater than the second argument.

The implementing program must ensure that all x and Y have sgn (compare (x, y)) = =-SGN (compare (y, x)). (This means that compare (x, y) must throw an exception if and only if compare (Y, x) throws an exception. )

The implementing program must also ensure that the relationship is transitive: ((compare (x, y) >0) && (compare (Y, z) >0) means compare (x, z) >0.

Finally, the implementing program must ensure that compare (x, y) ==0 means that for all z, there is sgn (compare (x, z)) ==sgn (compare (Y, z)).

Although this is common, it is not strictly required (compare (x, y) ==0) = = (X.equals (y)). Generally speaking, any comparator that violates this point should clearly point out this fact. The recommended language is "NOTE: This comparator forces a sort consistent with the equal sign." ”

Parameters: O1-The first object to compare. O2-The second object to compare. Returns: Negative integers, 0, or positive integers are returned with the first argument less than, equal to, or greater than the second argument. Thrown: classcastexception-If the type of the parameter does not allow this Comparator to be compared to them. Equals boolean equals(Objectobj) Indicates whether the other object "equals" this Comparator. This method must adhere to the general contract of Object.Equals (Object). In addition, this method returns true only if the specified Object is also a comparator and is forced to implement the same sort as this comparator. Therefore, comp1.equals (COMP2) means that sgn (Comp1.compare (O1, O2)) ==sgn (Comp2.compare, O1) are present for each object reference O1 and O2.

Note that it is always safe not to rewrite the Object.Equals (Object) method. However, in some cases, overriding this method allows the program to determine whether two different Comparator enforce the same sort, thereby improving performance.

Overwrite: equals parameter in class Object: Obj-the Reference object to compare. Return: Returns True only if the specified object is also a comparator and is forced to implement the same sort as this comparator. See also: Object.Equals (Java.lang.Object), Object.hashcode ()

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.