PackageCom.shiyu.client.frame.utils;Import java.util.*;ImportJava.util.Map.Entry;ImportJava.util.regex.Matcher;ImportJava.util.regex.Pattern;
/** * Created by the Administrator on 2017/1/3. */Public classcountsortlist {/** * list reverse order *@paramSTRs *@return */Public StaticList<string> countstr (list<string> strs) {map<string, integer> Map =NewHashmap<string, integer> (); Set<string> set =NewHashset<string> (STRs); for(String Str:set) { for(String lstr:strs) {if(Str.equals (LSTR)) {if(Map.containskey (str))
{Integer count = map.get (str);
count++;
Map.put (str, count); }Else{Map.put (str, 1);
}}} strs.clear (); map<string, integer> sortmap = sortmapbyvalue (map); for(Map.entry<string, Integer> entry:sortMap.entrySet ())
{Strs.add (Entry.getkey ()); } returnSTRs; /** * Map Reverse order *@paramMap *@return */private staticMap<string, integer> sortmapbyvalue (map<string, integer> Map) {list<map.entry<string, INTEGER&G T;> maplist =NewArraylist<map.entry<string, Integer>> (Map.entryset ()); Collections.sort (Maplist,NewComparator<map.entry<string, integer>> () {@Overridepublic intCompare (entry<string, integer> O1, entry<string, integer> O2) { returnO2.getvalue ()-O1.getvalue ();
}
}); map<string, integer> result =NewLinkedhashmap<string, integer> (); for(map.entry<string, integer> entry:maplist)
{Result.put (Entry.getkey (), Entry.getvalue ()); } returnResult } PublicMap<string, string> Sortmapbykey (map<string, string> orimap) {if(Orimap = =NULL|| Orimap.isempty ()) {return null; } map<string, string> sortedmap =NewTreemap<string, String> (NewComparator<string> () {public intCompare (String Key1, String key2) {intIntKey1 = 0, IntKey2 = 0;Try{intKey1 = GetInt (Key1);
IntKey2 = GetInt (Key2); }Catch(Exception e)
{intKey1 = 0;
IntKey2 = 0; } returnIntkey1-intkey2;
}
}); Sortedmap.putall (ORIMAP); returnSortedMap; }private intGetInt (String str) {inti = 0;Try{Pattern P = pattern.compile ("^\\d+ "); Matcher m = p.matcher (str);if(M.find ())
{i = integer.valueof (M.group ()); }
}Catch(NumberFormatException e)
{E.printstacktrace (); } returnI }Public static voidMain (string[] args) {list<string> STRs = arrays.aslist ("a","B","C","D","E","a","a","a","a","B","B","B"); map<string, integer> map =NewHashmap<string, integer> (); Set<string> set =NewHashset<string> (STRs); for(String Str:set) { for(String lstr:strs) {if(Str.equals (LSTR)) {if(Map.containskey (str))
{Integer count = map.get (str);
count++;
Map.put (str, count); }Else{Map.put (str, 1); }}} map<string, integer> sortmap = Sortmapbyvalue (MAP); for(Map.entry<string, Integer> entry:sortMap.entrySet ()) {System. out. println ("Key:"+ Entry.getkey () +"Value:" + Entry.getvalue ()); }
}
}