Java Comparator interface and javacomparator Interface

Source: Internet
Author: User
Tags java comparator

Java Comparator interface and javacomparator Interface

Comparator is located under the java. util package

public interface Comparator<T>
 

Forcibly perform collection on an objectOverall sorting. You can pass the Comparator to the sort method (for exampleCollections.sortOrArrays.sort) To allow precise control over the sorting order. You can also use Comparator to control certain data structures (suchOrdered setOrOrdered ing), Or for thoseNatural orderObject collection provides sorting.

If and only for a group of elementsSInE1AndE2For example,C. compare (e1, e2) = 0AndE1.equals (e2)ComparatorCForceSThe sorting is calledSame as equals.

Exercise caution when sorting an ordered set (or ordered ing) using a Comparator with a force sorting capability inconsistent with equals. Assume that an explicit ComparatorCOrdered set (or ordered ing) andSThe extracted elements (or keys) are used together. IfCForceSSorting is inconsistent with equals, so ordered set (or ordered ing) will act as "weird ". In particular, ordered set (or ordered ing) will violateEqualsThe general protocol of the defined set (or ing.

For example, assume that Comparator is usedcWill meet(a.equals(b) && c.compare(a, b) != 0)Two elementsaAndbAdd to an emptyTreeSet, The secondaddThe operation returns true (the size of the tree set will increase), because from the perspective of the tree set,aAndbIs not equal, even ifSet.addThe method has the opposite specification.

Note: In general, make Comparator also implementJava. io. SerializableIs a good idea, because they are in the serializable data structure (likeTreeSet,TreeMap) Can be used as a sorting method. To successfully serialize the data structure, Comparator (if provided) must implementSerializable.

In arithmetic terms, define the given ComparatorCSetSImplementationForcible sortingOfRelationalIs:

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

The overall sortingQuotient)Is:

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

It directly followsCompareAgreement, operator isSOnEquivalence relationship, Forced sorting isSOnOverall sorting. When we sayCForceSThe sorting isSame as equalsIt means that the sorting operator is an object.equals(Object)The equivalence relationship defined by the method:

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

This interface is a member of Java Collections Framework.

 

 

Start with the following versions:
1.2
For more information, see:
Comparable, Serializable

In java, how does the comparator interface compare strings?

If the return value is equal to 0, it indicates that the values are equal and the Unicode order is compared. Note that if the input values are numbers and numbers, the comparison results may be different.
Because "111" is before "2"

Usage of the Comparator interface in Java

I think what I said upstairs is not a concept that I want to ask. The landlord knows how to create a new class that has implemented interfaces, but does not quite understand why I can directly create an interface.

I will give you an easy-to-understand explanation. In fact, this sentence:
Comparator OrderIsdn = new Comparator (){

Equivalent to (not a standard syntax, but a description of the principle to help you understand ):
Comparator OrderIsdn = new (MyComp implements Comparator )(){

It's just because the Anonymous class has no name, so it's what you see.

In fact, the method you asked is the standard method of java. It means that I will implement this interface and create an object through this implementation. Note that the interface is not directly instantiated (although it looks like), but first implemented (followed by a pair of code in braces) and then instantiated.

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.