1. 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
1. What is the comparable interface?
This interface forcibly sorts the objects of each class. This sort is calledNatural sortingThe compareto method of the class is called itsNatural Comparison Method. Objects (and arrays) that implement this
Comparable introduction
Comparable is the sort interface.
If a class implements the comparable interface, it means "this class supports sorting." The class that implements the comparable interface supports sorting, assuming that there is now a
Delayqueue comparable interface and delayed interface off-thread pool digression-Static inner class
Delayqueue
Delayqueue is a kind of implementation class of JDK to Blockingqueue interface. For the introduction of Delayqueue, you can refer to a
Comparable interface: Let custom objects have a comparison ruleImportjava.util.ArrayList;Importjava.util.Collections;ImportJava.util.Comparator;/** Comparable interface: Allow custom objects to have comparison rules * **/ Public classComparabledemo {
The CompareTo method is not declared in object and is the only method in the comparable interface. CompareTo not only allows for simple equivalence comparisons, but also allows sequential comparisons to be performed .
Once the comparable interface
Java-collection framework Collections. sort (), Comparable interface, and Comparator interface, javasortcomparator
Collentions tool class -- java. util. Collections
Collentions is a tool class used to operate collection objects in the Java
I. The difference between comparable and comparator1, comparable and comparator are used to implement the collection of elements in the comparison, sorting.2. Comparable is the sort of method implementation defined within the class, located under
first, comparable interface, collections classCommon Algorithms for list:
Sort (List); If you need to sort your custom classes, you have to implement the comparable interface to implement a method that compares two class sizes
Shuffle
The Java comparator has two classes, namely the comparable interface and the comparator interface .When sorting an array of objects, the comparator works very clearly, first of all, to explain the comparable interface.
Let the object that needs to
Comparable Interface
Class that implements the comparable interface. objects can be compared between different sizes.The comparable interface has only one method.Public int compareto (Object OBJ );This method: returns 0 indicating this = OBJReturns
Java Comparator has two types: Comparable interface and Comparator interface.When sorting an array of objects, the comparator plays an obvious role. First, we will explain the Comparable interface.
Implement the Comparable interface for the objects
The comparable interface forces the overall ordering of objects that implement each of its classes. -Natural sort. The CompareTo of a class is called a natural comparison method. Public interface Comparable {public int compareTo (T o);}This
1. Comparator and comparable the same placeThey are all Java interfaces, and are used to compare the size of a custom class,What is a custom class: such as public class person{String name, int age}.When we have such a personlist, it contains Person1,
Java comparator has two types: comparable interface and comparator interface.
When sorting an array of objects, the comparator plays an obvious role. First, we will explain the comparable interface.
Implement the comparable interface for the objects
1. Comparator and comparable the same placeThey are all Java interfaces, and are used to compare the size of a custom class,What is a custom class: such as public class person{String name, int age}.When we have such a personlist, it contains Person1,
Comparison of the two interfaces:1: Comparable is the sorting of methods defined inside the set, and Comparator is the sorting implemented outside the set. The Comparable interface is really convenient for processing natural sorting, but there are
Example of Java Classic Programming 300 example 063 custom sort using comparable interface
case See http://blog.csdn.net/the_star_is_at/article/details/70245219
Example description
By default, arrays saved in the list collection are not sorted,
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.
First, use the comparable interface for sorting: How to sort a data type or a custom class must be implemented comparableBoth the base data type and the string type of data defined by the JDK are implemented comparable. The following example shows
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.