Java List Reverse Order

Source: Internet
Author: User
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 ()); }
    }
}
Related Article

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.