Java Basic Knowledge Hardening Collection Framework note 45:set Collection TreeSet store custom objects and traverse Exercise 1 (natural sort)

Source: Internet
Author: User

1. TreeSet Store your custom objects and traverse Exercise 1 (Natural sort):

(1)Student. Java:

1  Packagecn.itcast_05;2 3 /*4 * If an element of a class wants to be able to sort naturally, it must implement the natural sort interface5  */6  Public classStudentImplementsComparable<student> {7     PrivateString name;8     Private intAge ;9 Ten      PublicStudent () { One         Super(); A     } -  -      PublicStudent (String name,intAge ) { the         Super(); -          This. Name =name; -          This. Age =Age ; -     } +  -      PublicString GetName () { +         returnname; A     } at  -      Public voidsetName (String name) { -          This. Name =name; -     } -  -      Public intGetage () { in         returnAge ; -     } to  +      Public voidSetage (intAge ) { -          This. Age =Age ; the     } *  $ @OverridePanax Notoginseng      Public intCompareTo (Student s) { -         //return 0; the         //return 1; +         //return-1; A  the         //What is returned here should actually be done according to my sorting rules +         //sorted by age, the main conditions  -         intnum = This. Age-S.age; $         //Secondary Condition $         //at the same age, I have to see if my name is the same. -         //if age and name are the same, the same element -         intnum2 = num = = 0? This. Name.compareto (s.name): num; the         returnnum2; -     }Wuyi}

(2) test class TreeSetDemo2. Java:

1  Packagecn.itcast_05;2 3 ImportJava.util.TreeSet;4 5 /*6 * TreeSet stores custom objects and guarantees sorting and uniqueness. 7  * 8 * A: You didn't tell us how to sort9 * Natural sorting, sorted by age from small to largeTen * B: Element What's the only thing you didn't tell me? One * Member variable values are the same as the same element A  */ -  Public classTreeSetDemo2 { -      Public Static voidMain (string[] args) { the         //To create a collection object -treeset<student> ts =NewTreeset<student>(); -  -         //Creating Elements +Student S1 =NewStudent ("Linqingxia", 27); -Student s2 =NewStudent ("Zhangguorong", 29); +Student s3 =NewStudent ("Wanglihong", 23); AStudent S4 =NewStudent ("Linqingxia", 27); atStudent S5 =NewStudent ("Liushishi", 22); -Student s6 =NewStudent ("Wuqilong", 40); -Student s7 =NewStudent ("Fengqingy", 22); -  -         //adding elements - Ts.add (S1); in ts.add (S2); - Ts.add (S3); to Ts.add (S4); + Ts.add (S5); - Ts.add (S6); the Ts.add (S7); *  $         //TraversePanax Notoginseng          for(Student s:ts) { -System.out.println (S.getname () + "---" +s.getage ()); the         } +     } A}

Run the effect as follows:

Java Basic Knowledge Hardening Collection Framework note 45:set Collection TreeSet store custom objects and traverse Exercise 1 (natural sort)

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.