Java 17-8 Tressset Comparator ordering elements

Source: Internet
Author: User

1  PackageZl_treeset;2 3 ImportJava.util.Comparator;4 ImportJava.util.TreeSet;5 6 /*7 Store custom objects and traverse them with the Streeset collection. This time, sort by comparator .8 Public TreeSet (Comparator Comparator) Comparator sorting9 There are two ways of doing this:Ten 1: Create a class that comparator as an interface, One 2: If the parameter of a method is an interface, then what really wants is an object of the implementation class of the interface A and the anonymous inner class can implement this. -  - (new class name or interface name () { the override method; -  }  ); -  - Create a TreeSet collection, add animal objects, and sort by the following rules: + symbol name length short in front -  */ +  Public classTreeSetDemo2 { A  at      Public Static voidMain (string[] args) { -         //Public TreeSet (Comparator Comparator) Comparator sorting -         //Mode 1: Create a class that comparator as an interface, and then create -         //treeset<person> ts = new treeset<person> (new Mycomparator ()); -  -         //Mode 2: Internal anonymous class intreeset<person> ts =NewTreeset<person> (NewComparator<person>() { -              Public intCompare (Person A1, person A2) { to  +                 //Compare name Length -                 intNUM1 = A1.getname (). Length ()-a2.getname (). Length (); the  *                 //same length, then compare the name content $                 intnum2 = NUM1 = = 0?a1.getname (). CompareTo (A2.getname ())Panax Notoginseng : NUM1; -  the                 //name exactly the same, then the length of the province to Judge +                 intnum3 = Num2 = = 0?a1.getstate (). Length () A-a2.getstate (). Length (): num2; the  +                 //the same as the length of the province, then the content comparison -                 intNUM4 = Num3 = = 0?a1.getstate (). CompareTo (A2.getstate ()) $ : num3; $  -                 //It's all the same, age comparison. -                 intNUM5 = Num4 = = 0? A1.getage ()-a2.getage (): NUM4; the  -                 returnNUM5;Wuyi             } the         }); -  Wu         //To create a custom object -person P1 =NewPerson ("Big Bear", "Northeast", 21); Aboutperson P2 =NewPerson ("Bear II", "Sichuan", 17); $Person P3 =NewPerson ("Big Bear", "Fujian", 21); -Person P4 =NewPerson ("Big Bear", "Fujian", 20); -Person P5 =NewPerson ("Bear II", "Guangdong", 17); -Person P6 =NewPerson ("Big Bear", "Northeast", 21); APerson P7 =NewPerson ("Big Bear", "Fujian", 21); +  the         //add into Collection - Ts.add (p1); $ Ts.add (p2); the Ts.add (p3); the Ts.add (p4); the Ts.add (p5); the Ts.add (P6); - Ts.add (P7); in  the         //to traverse the          for(person p:ts) { AboutSystem.out.println (P.getname () + "\ T" + p.getstate () + "\ T" the+p.getage ()); the         } the  +     } -  the}

Java 17-8 Tressset Comparator ordering elements

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.