"Java Learning Notes" < collection framework >treeset,comparable,comparator

Source: Internet
Author: User

1  Public classPersonImplementscomparable{2     PrivateString name;3     Private intAge ;4     5      PublicPerson () {6         Super();7     }8      PublicPerson (String name,intAge )9     {Ten         Super(); One          This. Name =name; A          This. Age =Age ; -     } -      the      - @Override -      Public inthashcode () { -          +         returnName.hashcode () +age*27; -     } + @Override A      Public Booleanequals (Object obj) { at         if( This==obj) -             return true; -         if(! (objinstanceofPerson )) -             Throw NewClassCastException ("Type error"); -          -Person p =(person) obj; in          -         return  This. Name.equals (P.name) && This. Age = =P.age; to     } + @Override -      PublicString toString () { the          *         returnName+ ":" +Age ; $     }Panax Notoginseng      PublicString GetName () { -         returnname; the     } +      Public voidsetName (String name) { A          This. Name =name; the     } +      Public intGetage () { -         returnAge ; $     } $      Public voidSetage (intAge ) { -          This. Age =Age ; -     } the @Override -      Public intcompareTo (Object o) {//row by age, then by nameWuyi          thePerson p =(person) o; -          Wu         inttemp = This. Age-P.age; -          About         returnTemp==0? This. Name.compareto (p.name): temp; $          -          -     } -      A      +}

1 ImportJava.util.Comparator;2 3 ImportCn.itcast.p1.bean.Person;4 5  Public classComparatorbynameImplementsComparator {//First row by name, then by age line6 7 @Override8      Public intCompare (Object O1, Object O2) {9person P1 =(person) O1;Tenperson P2 =(person) O2; One          A         inttemp =p1.getname (). CompareTo (P2.getname ()); -  -         returnTemp==0?p1.getage ()-p2.getage (): temp; the     } -  -}


1 ImportJava.util.Iterator;2 ImportJava.util.TreeSet;3 4 ImportCn.itcast.p1.bean.Person;5 ImportCn.itcast.p5.comparator.ComparatorByName;6 7  Public classTreesetdemo {8 9      Public Static voidMain (string[] args) {Ten //demo1 (); OneTreeSet ts =NewTreeSet (Newcomparatorbyname ()); Add NewComparatorbyname (), there will beResults A, remove the words, appearResults 2
Comparator prior to comparable
A -Ts.add (NewPerson ("Zhangsan", 28)); -Ts.add (NewPerson ("Lisi", 21)); theTs.add (NewPerson ("Zhouqi", 29)); -Ts.add (NewPerson ("Zhaoliu", 25)); -Ts.add (NewPerson ("Wangwu", 24)); -Ts.add (NewPerson ("Lisi", 21)); +Ts.add (NewPerson ("Zhouqi", 29)); - +Iterator it =ts.iterator (); A at while(It.hasnext ()) - { -Person p =(person) it.next (); - - System.out.println (p); - } in - } to + Public Static voiddemo1 () { -TreeSet ts =NewTreeSet (); the *Ts.add ("abc")); $Ts.add ("Zaa");Panax NotoginsengTs.add ("AA"); -Ts.add ("NBA"); theTs.add ("CBA"); + AIterator it =ts.iterator (); the + while(It.hasnext ()) - { $ System.out.println (It.next ()); $ } - } - the}

"Java Learning Notes" < collection framework >treeset,comparable,comparator

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.