For the comparator interface and comparable interface, and their respective methods, compare (), compareTo (), and compareto
Two of the LeetCode questions today show the use of interfaces to sort objects. Links to the two questions are as follows:
1. Sort objects using the comparable Interface
2. sort by using the comparator Interface
Since I have never touched these two interfaces before and cannot underst
PHP development and production of a simple Activity Calendar calendar Calendar, calendar Calendar calendar
This book is actually an Activity Calendar.
This article is suitable for trans
String comparison is often used in projects, but sometimes there are many string operations and different rules. For example, in some cases, we need to use sorting rules, and in some cases case-insensitive. How can we write a comparison method that is easier to operate? Implementing the icomparer interface is a good solution.
Icomparable. compareto Method
On msdn, This is the explanation (machine translation:
Icomparable interface:Defines a type-spec
/*treeset* TreeSet automatically calls the element's CompareTo (Object obj) method after the data is stored and automatically sorts the data* So the output data is sorted data* Note: The return value of the CompareTo method is: Negative, 0, positive. Represent less than, equal to, greater than* CompareTo (Object obj) method to override element for storing custom
1. compareto is used for treeset, while treeset personally thinks it is best to read the DB result set. In the future, subset can be obtained based on compareto sorting results. When writing a database, you can use hashset instead of treeset, because the sorting and subset of the structure before writing do not need to be considered.
2. How to Implement compareto
In Java we often use the comparable interface to implement sorting, where CompareTo is implementing the interface method. We know that CompareTo returns 0 means that two objects are equal, a positive number represents greater than, and a negative number represents less than. At the same time we know that equals can also determine whether two objects are equal, then whether there is an association between th
In the past, compareTo () was used to compare the total size of ASCII codes. CompareTo (), which is used to compare the size of the corresponding characters (ASCII order) first. If the first character is different from the first character of the parameter, end the comparison and return the difference between them, if the first character is the same as the first character of the parameter, compare the second
Comparetopublic int compareto (string anotherstring) compares two strings alphabetically. This comparison is based on the Unicode values of each character in the string. Compare the Character Sequence represented by this string object with the character sequence represented by the parameter string. If the string object is in alphabetical order before the parameter string, the comparison result is a negative integer. If the string object is placed afte
"To achieve a natural ordering, the object collection must implement the comparable interface and override the CompareTo () method The general requirements describe the "main conditions", such as: Sort by name length. Note the secondary conditions such as: the length of the same time, the name of the content, age and other conditions are equal, which determines whether to deposit TreeSet collection.
PackageCn.itcast.day21.treeset;/** To achiev
These two days in the processing of payments to check the problem occurred, there is a comparison of the amount I used the bigdecimal equals method to compare two amounts are equal, resulting in the amount of errors (such as 3.0 and 3.00 comparison, etc.).
"NOTE: The following are all based on the Sun JDK version 1.4.2 As an example, the other implementations are not necessarily consistent, please ignore the"
First look at the BigDecimal equals method:
public boolean equals (Object x)
In project development, we all have this experience, that is, when adding a table, the corresponding increase in Java classes, in the Java class, there are a number of common methods, including: Equals (), Hashcode (), toString (), CompareTo () These four methods, For beginners who just touch Java, for this piece can not be more in-depth understanding, just the simple meaning of copy and paste, and not very good to understand.Now borrow this time to t
Interface comparableThis interface forces the overall ordering of the objects for each class that implements it. This sort is called the natural ordering of the class, and the compareTo method of the class is called its natural comparison method .The list of objects (and arrays) that implement this interface can be Collections.sort automatically sorted by (and Arrays.sort ). An object that implements this interface can be used as a key in an ordered m
What is the principle of string comparisons?Principle:Start by comparing the first character of the two strings (size comparison by the ASCII value of the characters) until a different character occurs or a '/s ' is encountered.If all characters are the same, the two strings are considered equal, returning 0;If there is a different character, the first character compares the result, if the former word sizes to the latter, then returns 1, otherwise returns-1.Attention:The order is the first objec
In the API source code, the CompareTo of string is actually an ASCII code that compares two strings at a time. If the ASCII equality of two strings continues the subsequent comparison, otherwise the two ASCII difference is returned directly. Returns 0 if two strings are exactly the same. The following is the source code: Public intcompareTo (String anotherstring) {intLen1 =count; intLen2 =Anotherstring.count; //gets the shorter length of the two strin
If the current time is July 19, 2007 13:56 47 secondsConvert.todatetime ("07/07/19"). CompareTo (DateTime.Now) The value returned at this time is "-1"Result: Convert.todatetime ("07/07/19") represents a time less than DateTime.NowReasonThe time represented by Convert.todatetime ("07/07/19") is: 2007-7-19 0:00:00DateTime.Now the time indicated: 2007-7-19 13:56:47If we now only do the exact "day" time comparison, then the two time to the same precision.
CompareAs can be seen from here, compare is a class in the comparator interface, and then look at the explanation in the source code
Compares its-arguments for order. Returns a negative integer,Zero, or a positive integer as the first argument is less than, equalTo, or greater than the second
If the first argument is less than the second parameter, a negative number is returned, or 0 if it is equal, and a positive number is returned if it is greater.Look at the
The Copareto method is used when learning to choose the sorting algorithm. Because of the comparison of the number, it is at random input.When I enter the string "8", it is compared to the string "16". String str1 = "8"; String str2 = "+"; String str3 = "n"; Console.WriteLine (Str1.compareto (STR2));//Results show 1 Console.WriteLine (Str3.compareto (ST
Write a book class that has at least the name and price two properties. To implement the comparable interface, the CompareTo () method of the interface specifies that the size relationship of two book class instances is the size of the price property of the two. In the main function, select the appropriate collection type to hold several objects of the book class, and then create an object of the new book class and check that the object is equal to wh
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.