Problems of Comparor and comparable in Java

Source: Internet
Author: User

Differences between compare () and CompareTo () methods in the comparable in 1.Comparator

TreeSet collection after the object is created,

A: If it is an empty structure, that is treeset<student> ts = new treeset<student> (); , then Ts.add (), when you need to implements the Comparable<student> interface on Student, rewrite the CompareTo () method below, since add () calls the CompareTo () method at this point, Bottom with red and black binary tree, after judging, sort

B: If it is with a parametric structure, i.e. treeset<student> ts = new treeset<student> (comparator<? super E>comparator); , Ts.add (), you need to write a Mycomparator class implements Comparator<student> interface, (if the call is less, you can create an object in the form of an internal class to override the method) or rewrite the Compare () method in Mycomparator.

C: Unresolved issues: why put

int num = S1.getname (). Length ()- s2.getname (). Length ();
(binary tree, left middle right order)
S1 and S2 change position, will be inverted sorted, binary tree, not big put on the right?
and the root node is the first number after changing position? If that's the case, then the judgment time
What is the number like?

1 ImportJava.util.Comparator;2 ImportJava.util.TreeSet;3 4 /*5 * Requirements: Sorted by name length6  * 7 * Public TreeSet (COMPARATOR<? Super e> Comparator): Constructs a new empty TreeSet, which is sorted according to the specified comparer. 8  */9  Public classTreesetdemo {Ten      Public Static voidMain (string[] args) { One         //To create a collection object A         //treeset<student> ts = new treeset<student> (); -         //Comparator Sort -         //treeset<student> ts = new treeset<student> (new Mycomparator ()); the         //form of the inner class -treeset<student> ts =NewTreeset<student> (NewComparator<student>() { - @Override -              Public intCompare (Student s1, Student S2) { +                 //Sort by the length of the name -                 intnum = S1.getname (). Length ()-s2.getname (). Length (); +                 //when the name is the same length, determine whether the name is equal A                 intnum2 = num = = 0?s1.getname (). CompareTo (S2.getname ()) at : num; -                 //when the name is the same, determine whether the age is equal -                 intnum3 = Num2 = = 0? S1.getage ()-s2.getage (): num2; -                 returnnum3; -             } -         }); in         //Create student Objects -Student S1 =NewStudent ("Little FSD name", 12); toStudent s2 =NewStudent ("Little SDF White", 22); +Student s3 =NewStudent ("Small SF Black", 33); -Student S4 =NewStudent ("Side dish", 13); theStudent S5 =NewStudent ("Little Black", 33); *Student s6 =NewStudent ("Little Sdfsdfc", 13); $Student s7 =NewStudent ("Little DSFSD Black", 33);Panax NotoginsengStudent S8 =NewStudent ("Little sfsd dish", 122); -Student S9 =NewStudent ("Little Black", 33); theStudent S10 =NewStudent ("Small C", 13); +  A         //adding elements the Ts.add (S1); + ts.add (S2); - Ts.add (S3); $ Ts.add (S4); $ Ts.add (S5); - Ts.add (S6); - Ts.add (S7); the Ts.add (S8); - Ts.add (S9);Wuyi Ts.add (S10); the  -         //traversing elements Wu          for(Student s:ts) { - System.out.println (s); About         } $     } -}
1 ImportJava.util.Comparator;2 3  Public classMycomparatorImplementsComparator<student> {4 5 @Override6      Public intCompare (Student s1, Student S2) {7         //Sort by the length of the name8         intnum = S1.getname (). Length ()-s2.getname (). Length ();9         //when the name is the same length, determine whether the name is equalTen         intnum2 = num = = 0?s1.getname (). CompareTo (S2.getname ()): num; One         //when the name is the same, determine whether the age is equal A         intnum3 = Num2 = = 0? S1.getage ()-s2.getage (): num2; -         returnnum3; -     } the  -}
1 //or public class Student implements comparable2  Public classStudent {3     //name4     PrivateString name;5     //Age6     Private intAge ;7 8      PublicStudent () {9         Super();Ten     } One  A      PublicStudent (String name,intAge ) { -         Super(); -          This. Name =name; the          This. Age =Age ; -     } -  -      PublicString GetName () { +         returnname; -     } +  A      Public voidsetName (String name) { at          This. Name =name; -     } -  -      Public intGetage () { -         returnAge ; -     } in  -      Public voidSetage (intAge ) { to          This. Age =Age ; +     } -  the @Override *      PublicString toString () { $         return"Student [name=" + name + ", age=" + Age + "]";Panax Notoginseng     } -  the     //@Override +     //public int CompareTo (Student s) { A     // //Sort by the length of the name the     //int num = this.name.length ()-s.name.length (); +     // //when the name is the same length, determine whether the name is equal -     //int num2 = num = = 0? this.getname (). CompareTo (S.getname ()): num; $     // //when the name is the same, determine whether the age is equal $     //int num3 = Num2 = = 0? this.getage ()-This.getage (): num2; -     //return num3; -     // } the}

Problems of Comparor and comparable in Java

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.