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.sort
OrArrays.sort
) To allow precise control over the sorting order. You can also use Comparator to control certain data structures (suchOrdered set
OrOrdered ing
), Or for thoseNatural order
Object 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 usedc
Will meet(a.equals(b) && c.compare(a, b) != 0)
Two elementsa
Andb
Add to an emptyTreeSet
, The secondadd
The operation returns true (the size of the tree set will increase), because from the perspective of the tree set,a
Andb
Is not equal, even ifSet.add
The 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.