TreeSet (non-repeatable, automatic sorting) for custom sorting

Source: Internet
Author: User
Tags comparable

Method One: Let the class implement the comparable interface, and overwrite the CompareTo () method, add the custom class to TreeSet

1 ImportJava.util.Scanner;2 ImportJava.util.TreeSet;3 //TreeSet Applications4 classMain {5      Public Static voidMain (string[] args) {6Scanner s =NewScanner (system.in);7Treeset<stringdemo> set =NewTreeset<>();8          while(S.hasnextline ()) {9             intn =Integer.parseint (S.nextline ());Ten              for(inti = 0; I < n; i++) { OneString string =s.nextline (); ASet.add (NewStringdemo (String)); -             } -              for(Stringdemo s1:set) { the System.out.println (s1.getstring ()); -             } -         } - s.close (); +     } - } + classStringdemoImplementscomparable{ A     Privatestring string; at Stringdemo (String s) { -          This. String =s; -     } -      Public intcompareTo (Object obj) { -Stringdemo s =(Stringdemo) obj; -         if( This. String.CompareTo (s.string) <= 0) in             return-1; -         return1; to     } +      PublicString getString () { -         return  This. String; the     } *}
View Code

Method Two: Customize a sequencer that implements comparator, implement the Compare () method (two comparisons), add this sequencer when creating treeset

1 ImportJava.util.Comparator;2 ImportJava.util.Scanner;3 ImportJava.util.TreeSet;4 //TreeSet Applications5 classMain {6      Public Static voidMain (string[] args) {7Scanner s =NewScanner (system.in);8Treeset<string> set =NewTreeset<> (Newstrcomparator ());9          while(S.hasnextline ()) {Ten             intn =Integer.parseint (S.nextline ()); One              for(inti = 0; I < n; i++) { AString string =s.nextline (); - Set.add (string); -             } the              for(String s1:set) { - System.out.println (S1); -             } - set.clear (); +         } - s.close (); +     } A } at classStrcomparatorImplementscomparator{ -      Public intCompare (Object Obj1, Object obj2) { -String S1 =(String) obj1; -String s2 =(String) obj2; -         if(S1.compareto (S2) <= 0) -             return-1; in         return1; -     } to}
View Code

TreeSet (non-repeatable, automatic sorting) for custom sorting

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.