TreeMap Chinese sort, treemap reverse output arrangement

Source: Internet
Author: User
Tags collator

1, TreeMap set in reverse order
ImportJava.util.Comparator;/*** Compare algorithm classes, comparators *@authorAdministrator **/ Public classmycmpImplementsComparator<object> {    //Achieve Reverse@Override Public intCompare (Object O1, Object O2) {intx =o2.tostring (). CompareTo (O1.tostring ()); returnx; }}
ImportJava.util.Iterator;ImportJava.util.TreeMap; Public classTestMap { Public Static voidMain (string[] args) {TreeMap<String,String> map =NewTreemap<string,string> (Newmycmp ()); Map.put ("10001", "Zhang MoU"); Map.put ("10005", "Sun MoU"); Map.put ("10003", "Fang"); Map.put ("10004", "Tan MoU"); Map.put ("10002", "Lee"); Iterator<String> it =Map.keyset (). iterator ();  while(It.hasnext ()) {String key=It.next (); SYSTEM.OUT.PRINTLN (Key+"="+Map.get (key)); }    }}

2, TreeMap Chinese sort
ImportJava.text.CollationKey;ImportJava.text.Collator;ImportJava.util.Comparator;/*** Handle Chinese sort *@authorAdministrator **/ Public classMyCmp1ImplementsComparator<object>{Collator Collator=collator.getinstance (); /*** provides classes and interfaces for handling text, dates, numbers, and messages in a way that is not related to natural language * Get the collator of the current default locale*/@Override Public intCompare (Object O1, Object O2) {//TODO auto-generated Method StubCollationkey Key1 =Collator.getcollationkey (o1.tostring ()); Collationkey Key2=Collator.getcollationkey (o2.tostring ()); returnKey1.compareto (Key2); }}
ImportJava.util.Iterator;ImportJava.util.Map.Entry;ImportJava.util.Set;ImportJava.util.TreeMap; Public classTestMap1 { Public Static voidMain (string[] args) {TreeMap<String,String> map =NewTreemap<string,string> (NewMYCMP1 ()); Map.put ("Alan", "1001"); Map.put ("Past", "1002"); Map.put ("After", "1003"); Map.put ("Tomorrow", "1004"); Map.put ("Future", "1005"); Set<Entry<String,String>> set =Map.entryset (); Iterator<Entry<String,String>> it =Set.iterator ();  while(It.hasnext ()) {Entry<string, string> e =It.next (); System.out.println (E.getkey ()+"="+E.getvalue ()); }    }}

TreeMap Chinese sort, treemap reverse output arrangement

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.