1. TreeSet guarantee Element uniqueness and comparator ordering principle and code implementation (comparator sort)
(1)Student. Java:
1 Packagecn.itcast_07;2 3 Public classStudent {4 PrivateString name;5 Private intAge ;6 7 PublicStudent () {8 Super();9 }Ten One PublicStudent (String name,intAge ) { A Super(); - This. Name =name; - This. Age =Age ; the } - - PublicString GetName () { - returnname; + } - + Public voidsetName (String name) { A This. Name =name; at } - - Public intGetage () { - returnAge ; - } - in Public voidSetage (intAge ) { - This. Age =Age ; to } +}
(2)Treesetdemo. Java:
1 Packagecn.itcast_07;2 3 ImportJava.util.Comparator;4 ImportJava.util.TreeSet;5 6 /*7 * Requirements: Please follow the name of the length of the order8 * 9 * TreeSet Sets the principle of guaranteed element ordering and uniquenessTen * Uniqueness: is based on whether the return of the comparison is determined by the. One * Sort: A * A: Natural sorting (elements are comparative) - * Allow the class that the element belongs to to implement the natural sort interface comparable - * B: Comparator sort (set with comparison) the * Let the construction method of the collection receive a subclass object of the comparer interface Comparator - */ - Public classTreesetdemo { - Public Static voidMain (string[] args) { + //To create a collection object - //treeset<student> ts = new treeset<student> ();//Natural Sort + //Public TreeSet (Comparator Comparator)//Comparator Sort A //treeset<student> ts = new treeset<student> (new Mycomparator ()); at - //if the parameter of a method is an interface, then the object that really wants to be the implementation class of the interface - //and the anonymous inner class can implement this. -treeset<student> ts =NewTreeset<student> (NewComparator<student>() { - @Override - Public intCompare (Student s1, Student S2) { in //name Length - intnum = S1.getname (). Length ()-s2.getname (). Length (); to //name Content + intnum2 = num = = 0?s1.getname (). CompareTo (S2.getname ()) - : num; the //Age * intnum3 = Num2 = = 0? S1.getage ()-s2.getage (): num2; $ returnnum3;Panax Notoginseng } - }); the + //Creating Elements AStudent S1 =NewStudent ("Linqingxia", 27); theStudent s2 =NewStudent ("Zhangguorong", 29); +Student s3 =NewStudent ("Wanglihong", 23); -Student S4 =NewStudent ("Linqingxia", 27); $Student S5 =NewStudent ("Liushishi", 22); $Student s6 =NewStudent ("Wuqilong", 40); -Student s7 =NewStudent ("Fengqingy", 22); -Student S8 =NewStudent ("Linqingxia", 29); the - //adding elementsWuyi Ts.add (S1); the ts.add (S2); - Ts.add (S3); Wu Ts.add (S4); - Ts.add (S5); About Ts.add (S6); $ Ts.add (S7); - Ts.add (S8); - - //Traverse A for(Student s:ts) { +System.out.println (S.getname () + "---" +s.getage ()); the } - } $}
The results of the operation are as follows:
Java basic Knowledge Hardening Collection Framework note 47:set set TreeSet guarantee element uniqueness and comparator ordering principle and code implementation (comparator sort)