Tag: null override cannot serial failed RBO entity i++ dataAfter reading this article you will learn:
Comparable natural sort
Comparator Custom Sorting
Summarize
There are two comparison mechanisms available in Java: Comparable and Comparator, what's the difference between them? Come and find out today.Comparable natur
comparable and comparator in Java
-comparable interfaceInterface definitionpublicinterface ComparableT> { publicint compareTo(T o); } The interface contains only one CompareTo () function,int CompareTo (object o): Compares the current instance object with the object o, returns a negative value if it precedes the object o, returns 0 if two objects are in the same position in the sort, and retu
Comparable and Comparator in java
Comparable and Comparator in java
-Comparable Interface
Interface Definition
public interface Comparable
{ public int compareTo(T o); }
This interface only contains a compareTO () function,
Int compareTo (Object o): compares the current instance Object with the
comparison method for the item object@Override Public int CompareTo(Item Other) {returnInteger.compare (PartNumber, Other.partnumber); }}(2) comprehensive comparable and comparator//Implement comparable interface Public classPerson implements comparablePrivate intNumPrivateString name;Private intAge Public Person(intNum, String name,intAge) {super (); This. num = num; This. name = name; This. Age = Age;} Public int CompareTo(Object o) {return This.
Comparison between Java Comparator and Comparable
1. How to sort objects by comparison?
Java basic types such as int, float, and double can be directly compared and sorted using java. util. Arrays. sort (Object []). How can we compare and sort objects? The classes we define inherit the Object class. The equals method o
the order of a class, and the class itself does not support ordering (that is, it does not implement the comparable interface), then we can create a "comparer for this class" to sort by. This "comparator" only needs to implement the comparator interface.That is, we can create a new comparer by implementing the comparator class , and then sort the classes through
;import java.util.collection;import java.util.collections;import java.util.Comparator; Import Java.util.hashset;import java.util.list;import java.util.sortedset;import java.util.TreeSet; public class listsortwithcomparator{publicstatic void Main (string[] args) {/* * Use comparator to sort the first Method, * Define a class that implements the comparator interface, and overload the Compara method, set the c
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, Person2, Persion3 ...., we use Collections.sort (personlist),is not getting the expected results. At this point someone must ask, why you can sort a string lis
"comparer for that class" to sort by.Comparable is equivalent to an "internal comparator", while comparator is equivalent to an "external comparator".Each of the two methods have advantages and disadvantages, with comparable simple, as long as the implementation of the comparable interface object directly becomes a comparable object, but need to modify the sourc
object class has implemented the Equals methodThe comparable interface provides only the int compareTo (T O) method, which means that if I define a person class that implements the comparable interface, then when I instantiate the person1 of the person class, I want to compare person1 and The size of an existing person object Person2, I can call it this way: Person1.comparto (Person2), can be judged by the return value, and at this point if you define a Personcomparator (implements the
Java Comparator interface and javacomparator Interface
Comparator is located under the java. util package
public interface Comparator
Forcibly perform collection on an objectOverall sorting. You can pass the Comparator to the sor
());int v2 = Math.abs(((Integer) o2).intValue());return v1 > v2 ? 1 : (v1 == v2 ? 0 : -1);}}
Use AbsComparator to sort the set:
Import java. util. *; public class Test {public static void main (String [] args) {// generate an array of 20 Random integers (with positive and negative values) Random rnd = new Random (); integer [] integers = new Integer [20]; for (int I = 0; I
Use the Comparable interface to complete sorting: The list of objects (and
interface is to define a custom class's comparison rules, because the CompareTo method has only one parameter and the other parameter is this, which is the class itself, so we can think of it as defining a comparison rule within the class. The equivalent of a Java developer writing a string class should allow it to implement the comparable interface. However, the Compare method has two parameters, which can be of any type, either a string type that h
particular, ordered set (or ordered mapping) will violate the general contract of set (or mapping) defined by equals .For example, assuming that you use Comparator to c satisfy (a.equals(b) c.compare(a, b) != 0) two elements a and b add to an empty TreeSet , the second add operation will return True (the size of the tree set will increase) because from the point of view of the tree set , a and b are unequal, even if this is Set.add contrary to the s
Comparableand is the Comparator interfaces provided by Java Core API. From their names, we can tell they is used for comparing stuff in some. But what exactly was they and what is the difference between them? The following is examples for answering this question. The simple examples compare the size of the HDTV ' s. How to use comparable vs. Comparator are obviou
after being added to the list container, or the comparable interface may be implemented, and if comparator is not specified in the sort method of the collections class, it is sorted in a natural order. The so-called natural order is the implementation of the comparable interface settings of the ordering mode.Comparator is a dedicated comparator that can write a compara
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 list (or array) of objects for the class that implements the comparable interface, the list (or array) can be passed through Collections.sort (or Arrays.sort) to sort.
In addition, the object of the class implementing the comparable interface can be use
Same
Both comparable and comparator are used to implement object comparisons, sorting
To compare and sort objects, you need to implement comparable or comparator interfaces
Comparable and comparator are all Java interfaces.
Difference
The comparator
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.