Java Collection Framework (TreeSet operation, automatic sorting of data, overriding CompareTo method)

Source: Internet
Author: User
Tags comparable

/*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. Represent less than, equal to, greater than
* CompareTo (Object obj) method to override element for storing custom object elements
* When defining an element, you need to implement the comparable interface
* */

1 ImportJava.util.Iterator;2 ImportJava.util.TreeSet;3  Public classStudentcode {4     5      Public Static voidMain (String []args) {6         //defines a TreeSet object and assigns a value to the object that exists in Java7TreeSet ts1=NewTreeSet ();8Ts1.add ("java10");9Ts1.add ("Java01");TenTs1.add ("Java08"); OneTs1.add ("Java04"); A         //the value of the output object, which is the sorted data - System.out.println (TS1); -         //define a TreeSet object and assign a custom object theTreeSet ts2=NewTreeSet (); -Ts2.add (NewPerson ("LS", 11)); -Ts2.add (NewPerson ("ZS", 22)); -Ts2.add (NewPerson ("LS", 13)); +Ts2.add (NewPerson ("LS", 11)); -         //output object, also sorted data +          for(Iterator it=ts2.iterator (); It.hasnext ();) { APerson p=(person) it.next (); atSystem.out.println ("Name:" +p.getname () + ", Age:" +p.getage ()); -         } -     } - } - //custom data, need to implement comparable interface - classPersonImplementscomparable{ in     PrivateString name; -     Private intAge ; toPerson (String name,intAge ) { +          This. name=name; -          This. age=Age ; the     } *      PublicString GetName () { $         return  This. Name;Panax Notoginseng     } -      Public intGetage () { the         return  This. Age; +     } A     //override the CompareTo () method, the      Public intcompareTo (Object obj) { +         if(! (objinstanceofPerson )) -             Throw NewRuntimeException ("Not Person object"); $Person p =(person) obj; $         if( This.age>P.getage ()) { -             return1; -         } the         Else if( This.age<P.getage ()) { -             return-1;Wuyi}Else{ the             return  This. Name.compareto (P.getname ()); -         } Wu     } -}

1 /* TreeSet 2 *     TreeSet when the element is not comparable, or if the comparison is not required,  3  *         can make the TreeSet set a comparative feature.  4  *         define the comparator and pass the comparator as a parameter to the TreeSet collection 5  * The         comparator needs to implement the comparator interface 6 *     When the element is comparable and the comparator appears at the same time, the comparator will prevail.  7* /
8 ImportJava.util.Comparator;9 ImportJava.util.Iterator;Ten ImportJava.util.TreeSet; One  Public classStudentcode { A      -      Public Static voidMain (String []args) { -         //define the TreeSet object and pass in the comparer theTreeSet ts2=NewTreeSet (Newmycompareble ()); -Ts2.add (NewPerson ("LS", 11)); -Ts2.add (NewPerson ("ZS", 22)); -Ts2.add (NewPerson ("LS", 13)); +Ts2.add (NewPerson ("LS", 11)); -         //Output Object +          for(Iterator it=ts2.iterator (); It.hasnext ();) { APerson p=(person) it.next (); atSystem.out.println ("Name:" +p.getname () + ", Age:" +p.getage ()); -         } -     } - } - //define comparators--First order in name, age in second order - classMycomparebleImplementscomparator{ in     //implement the Compare method inside the comparator -      Public intCompare (Object o1,object O2) { toPerson p1=(person) O1; +Person p2=(person) O2; -         intnum=p1.getname (). CompareTo (P2.getname ()); the         if(num==0){ *             return NewInteger (P1.getage ()). CompareTo (NewInteger (P2.getage ())); $         }Panax Notoginseng         returnnum; -     } the } +  A //custom data, need to implement comparable interface--in the first order of age, the name is the second order the classPersonImplementscomparable{ +     PrivateString name; -     Private intAge ; $Person (String name,intAge ) { $          This. name=name; -          This. age=Age ; -     } the      PublicString GetName () { -         return  This. Name;Wuyi     } the      Public intGetage () { -         return  This. Age; Wu     } -     //override the CompareTo () method, About      Public intcompareTo (Object obj) { $         if(! (objinstanceofPerson )) -             Throw NewRuntimeException ("Not Person object"); -Person p =(person) obj; -         if( This.age>P.getage ()) { A             return1; +         } the         Else if( This.age<P.getage ()) { -             return-1; $}Else{ the             return  This. Name.compareto (P.getname ()); the         } the     } the}

Java Collection Framework (TreeSet operation, automatic sorting of data, overriding CompareTo method)

Related Article

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.