Explain in detail the differences between comparable and comparator interfaces in Java

Explain in detail the differences between comparable and comparator interfaces in Java

This article will analyze the differences between comparable and comparator interfaces in Java in detail. Both have comparative functions. What are the differences? Interested Java developers continue to look at them.

Introduction to comparable

Comparable is a sort interface.

If a class implements the comparable interface, it means that "this class supports sorting". That is, the class implementing the comparable interface supports sorting. Assuming that there is a "list (or array) of objects of the class implementing the comparable interface", the list (or array) can be sorted through collections Sort (or arrays. Sort).

In addition, the object of the class implementing the comparable interface can be used as a key in an ordered map (such as treemap) or an element in an ordered set (TreeSet), without specifying a comparator.

Comparable definition

The comparable interface only includes one function, which is defined as follows:

explain:

Suppose we "compare the size of X and Y" through X. CompareTo (y). If "negative number" is returned, it means "x is smaller than y"; Return "zero", which means "x equals y"; Returns a positive number, meaning "x is greater than y".

Introduction to comparator

Comparator is the comparator interface.

If we need to control the order of a class, and the class itself does not support sorting (that is, it does not implement the comparable interface); Then, we can create a "comparator of this class" to sort. This "comparator" only needs to implement the comparator interface.

In other words, we can "create a new comparator by implementing the comparator class", and then sort the classes through the comparator.

Comparator definition

Comparator interface only includes two functions, which are defined as follows:

explain:

(01) if a class wants to implement the comparator interface: it must implement the CompareTo (t O1, t O2) function, but it may not implement the equals (object obj) function.

Why not implement the equals (object obj) function? Because any class has implemented equals (object obj) by default. All classes in Java inherit from Java Lang.Object, in object The equals (object obj) function is implemented in Java; Therefore, all other classes also implement the function.

(02) int compare (t O1, t O2) is "compare the size of O1 and O2". Return "negative number", which means "O1 is smaller than O2"; Return "zero", which means "O1 equals O2"; Returns a positive number, meaning O1 is greater than O2.

Comparator and comparable comparison

Comparable is the sorting interface; If a class implements the comparable interface, it means that "this class supports sorting".

Comparator is a comparator; If we need to control the order of a class, we can create a "comparator of this class" to sort.

It is not difficult to find that comparable is equivalent to "internal comparator" and comparator is equivalent to "external comparator".

We describe the two interfaces through a test program. The source code is as follows:

This procedure is described below.

a) Person class definition. As follows:

explain:

(01) the person class represents a person. The person class has two attributes: age (age) and name "person name". (02) the person class implements the comparable interface, so it can be sorted.

b) In main (), we create the list array (list) of person. As follows:

c) Next, we print out all the elements of the list. As follows:

d) Then, we sort the list through the sort () function of collections.

Because person implements the comparable interface, when sorting through sort (), it will be sorted according to the sorting method supported by person, that is, the rules defined by CompareTo (person). As follows:

e) Compare comparable and comparator

We define two comparators, ascagecomparator and descagecomparator, to sort people in ascending and descending order respectively.

e. 1) ascagecomparator

It sorts people in ascending order by age. The code is as follows:

e. 2) descagecomparator

It sorts people in descending order by age. The code is as follows:

f) Operation results

Run the program and the output is as follows:

If you have any questions, please leave a message or go to the community of this site for exchange and discussion. Thank you for reading. I hope it can help you. Thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>