Here is a brief introduction to Java's comparable internal comparator and comparator external comparator usage implementationSo let's do a sort of between objects, first build a model or call Javaben. As follows:The use of the 1.Java comparable internal comparator is impleme
defined as follows:
Package java.util;
Public interface comparator
Description1. If a class implements the comparator interface: it must implement the CompareTo (t O1, T O2) function, but it may not implement the Equals (Object obj) function.Why not implement the Equals (Object obj) function? Because any class, the default is already implemented equals (Object obj). All classes in
[Common Java class libraries] _ notes on comparator (comparable and comparator)
Objectives of this chapter:Understanding the use of comparable comparison InterfacesUnderstand the basic sorting principle of ComparatorUnderstanding the use of comparator comparison Interfaces
Comparable Interface
You can directly use the
second list and comparer method to sort key or value, if the comparator returns 0, the value is the same as the reverse value, how much output. (If a friend knows why, please leave a message under the article, thank you)HashMap sortWe are all HashMap values are not sequential, he is based on key hashcode to achieve. How are we going to achieve this sort of unordered hashmap? Referring to the value of TreeMap, we can also implement HashMap sort.Sort b
O2, returns 0 for O1 and O2 equal, and returns a positive number that represents O1 greater than O2.Publicclasstreesettest{publicstaticvoidmain (String []args) {treesettree=newtreeset ( Newmycomparator ()); tree.add (NewPerson); tree.add (Newperson); tree.add (Newperson); tree.add (new Person ()); System.out.println (tree); //[20,21,24,33]}}classperson{ intage;stringname;public person (intage) {this.age=age; }Nbsp;publicstringtostring () {return string.valueof (this.age);}}classmycomparatorimpl
Comparator is located under Package Java.util, the comparator, which is defined in the collection outside the sorting.Comparable is located under Package Java.lang, which represents the comparison of the current object, and is the implementation of sorting within the collection.Comparable represents an object that supports ordering internally (such as the String class, the integer class, the internal imple
binarytree{Class node{//Declaration of a Node classprivate comparable data; Save the specific contentPrivate Node left; Save ZuoziPrivate Node right; Save Right subtreePublic Node (comparable data) {This.data = data;}public void AddNode (Node newNode) {Make sure it's on the Zuozi or right subtree.if (NewNode.data.compareTo (this.data) if (this.left==null) {This.left = NewNode; Set the new node directly to Zuozi}else{This.left.addNode (NewNode); Continue judging down}}if (NewNode.data.compareTo
".
Comparator Introduction
Comparator is the comparator interface.
If we need to control the order of a class, and the class itself does not support sorting (that is, not implementing the comparable interface), then we can create a "comparer for that class" to sort. This "comparer" only needs to implement the comparator
IntroductionThe Comparator is the comparator interface.If we need to control 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
Comparable and comparator interface difference analysis in JavaSource: Code Farming Network | Time: 2015-03-16-10:25:20 | Hits:8902
[Introduction] This article is to be detailed analysis of Java comparable and comparator interface differences, both have a comparative function, then what is the difference, interested
classes in Java inherit from Java.lang.Object and implement the Equals (Object obj) function in Object.java, so all other classes are equivalent to implementing the function.() int compare (t O1, T O2) is "Comparing the size of O1 and O2". Returns "negative number", meaning "O1 is smaller than O2"; returning "0" means "O1 equals O2"; returning "positive number" means "O1 is greater than O2".
comparison of C
The details and differences of Java comparable and Comparator
Java provides us with two comparison mechanisms: comparable and Comparator, what is the difference between them? Come to understand today.
Comparable natural sort
Comparable under the Java.lang package, is an interface with only one internal method Compar
(Object obj) function? Because any class, the default is already implemented equals (Object obj). All classes in Java inherit from Java.lang.Object and implement the Equals (Object obj) function in Object.java, so all other classes are equivalent to implementing the function.
The int compare (t O1, T O2) is the size of the comparison O1 and O2. Returning "negative numbers" means "O1 is smaller than O2"; returning "0" means "O1 equals O2";
and assign a value to an object to write the collation for the custom class in the Compare method2. Passing a Comparator object as a parameter to a method of the Sort class3. Add the custom class used in the Compare method to the Sort classExamples of interface operations:Import Java.util.arraylist;import Java.util.collections;import Java.util.comparator;public class Car {public String name;public int price;public Car (String name, int price) {this.n
equivalence relation defined by the Equals (object) method of the object:
{(x, y) such that x.equals((Object)y)}.
This interface is a member of the Java collections Framework.
Starting from the following version: 1.2 See also: Comparable, Arrays.sort (object[], Comparator), TreeMap, TreeSet, SortedMap, SortedSet, Serializable
Method Summary
Int
Compare (To1, To2)Compares th
In Java, when sorting data, comparable and comparator are not needed to be met. Ordinary string, integer and other types have implemented the comparable interface, and sometimes we need to sort some other classes that do not have an intrinsic sort relationship, then we need to implement the above two interfaces, but it will be different.Before searching for the relationship between the two. See such an arti
. This method compares two objects based on a certain policy and returns an integer. The interface is defined as follows: The code is as follows:Copy code Int compare (T o1, T o2 ); If o1 is less than o2,-1 is returned; if o1 is equal to o2, 0 is returned; if o1 is greater than o2, 1 is returned.Based on this principle, we implement the following Comparator interface code: The code is as follows:Copy code Package com. guoweiwei. test.
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.