By default, the Equals (object o) method of the Object (provided by the Base Class object) is used to compare whether two Object variables reference the same object.
We must define our own object comparison methods.
The IComparable and ICompare
By default, the equals (Object O) method of the object (provided by the Base Class Object) is used to compare whether two object variables reference the same object.
We must define our own object comparison methods.
The icomparable and
Objective
The comparison of two objects "size" is compared when the algorithm is performed to insert sort, hill sort, and merge sort. It's easy to understand the i>j of integer types, but how do we compare the size of two objects when we sort
Recommendation 10: Consider whether to implement a comparer when creating objectsThere are comparisons where objects exist, as in the world of. Net. For the simplest example, there is a salary list of 10 people in the UI. Depending on the sort needs,
customize a comparer to sort the elements in the TreeSet collection by the specified methodImport java.util.comparator;import java.util.iterator;import java.util.treeset;//Customize a comparator class Mycompare implements comparator{@Overridepublic
I. Comparator comparable and comparatorThe previous blog introduced the Tool class arrays tool class. We can call the Arrays.sort () function on an array of basic types to sort the array. Sorting operations are often used in daily development. Then
I. Comparator comparable and comparatorThe previous blog introduced the tool class arrays tool class . We can call the Arrays.sort () function on an array of basic types to sort the array. Sorting operations are often used in daily development. Then
I. Comparator comparable and comparatorThe previous blog introduced the Tool class arrays tool class. We can call the Arrays.sort () function on an array of basic types to sort the array. Sorting operations are often used in daily development. Then
In real-world projects, you often encounter sorting problems, sorting the collection for basic data type Java support Arrays.sort () and Collection.sort (), but sorting the user-defined types? Java provides us with two solutions.
One: By
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.