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