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
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.
In today's leetcode, there are two of them. The use of interfaces to implement the ordering of objects. RELATED links to the two questions:1. Sorting objects using the comparable interface2. Using comparator interface to achieve sortingBecause the
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
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
/*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.
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
Comparable1. What is the comparable interface?This interface forces the overall ordering of the objects for each class that implements it. This sort is called the natural sort of the class, and the compareTo method of the class is called its natural
A first exampleJava code
/* To compare, let your class implement the comparable interface and rewrite it in the sort order you want CompareTo
*map just provides a sort of key, but when we need to sort the values, we provide our own comparators.
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,
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.