Java Learning Notes < collection frames > sorting strings by length

Source: Internet
Author: User

1  Packagetesting;2 3 ImportJava.util.Comparator;4 5  Public classComparatorbylengthImplementsComparator {//defining the Comparer6 7 @Override8      Public intCompare (Object O1, Object O2) {9String S1 =(String) O1;TenString s2 =(String) O2; One          A         inttemp = S1.length ()-s2.length (); -          -         returnTemp==0?S1.compareto (S2): temp; the     } -  -}
1 ImportJava.util.Iterator;2 ImportJava.util.TreeSet;3 4  Public classTreesettest {5 6      Public Static voidMain (string[] args) {7         8TreeSet ts =NewTreeSet (Newcomparatorbylength ());9         TenTs.add ("Aaaaaaaa"); OneTs.add ("ZZ"); ATs.add ("Nbaq"); -Ts.add ("CBA"); -Ts.add ("abc")); the          -Iterator it =ts.iterator (); -          -          while(It.hasnext ()) +         { - System.out.println (It.next ()); +         } A  at     } -  -}

Java Learning Notes < collection frames > sorting strings by length

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.