The Java language uses Collections.sort to sort map,list

Source: Internet
Author: User

1.main method contains TreeMap sort 1,treemap sort 2,hashmap sort,list<integer> sort,list<bean> sort,list<map> sort

Package Com.tao.test;import java.util.arraylist;import java.util.collections;import java.util.Comparator; Import Java.util.hashmap;import java.util.list;import Java.util.map;import Java.util.map.entry;import Java.util.TreeMap; Public classSort { Public Static voidMain (string[] args) {//TreeMap Sort 1map<string, string> TreeMap =NewTreemap<string, String> (NewComparator<string>() {             Public intCompare (String O1, String O2) {//Sort Ascending                returnO1.compareto (O2);        }        }); Treemap.put ("C","CCCCC"); Treemap.put ("a","AAAAA"); Treemap.put ("b","bbbbb"); Treemap.put ("D","ddddd"); //post-order output         for(String key:treeMap.keySet ()) {System. out. println ("key="+ key +", value="+ TreeMap.Get(key)); }        //TreeMap Sort 2map<string, integer> treeMap2 =NewTreemap<string, integer>(); Treemap2.put ("D",3); Treemap2.put ("b",4); Treemap2.put ("a",7); Treemap2.put ("C",1); //Convert to Listlist<map.entry<string, integer>> treelist =NewArraylist<map.entry<string, integer>>(Treemap2.entryset ()); Collections.sort (Treelist,NewComparator<map.entry<string, integer>>() {            //Sort Ascending             Public intCompare (entry<string, integer> O1, entry<string, integer>O2) {                returnO1.getvalue (). CompareTo (O2.getvalue ());        }        }); //post-order output         for(Map.entry<string, integer>m:treelist) {System. out. println ("key="+ M.getkey () +", value="+M.getvalue ()); }        //HashMap Sortmap<string, integer> HashMap =NewHashmap<string, integer>(); Hashmap.put ("C",3); Hashmap.put ("a",2); Hashmap.put ("b",1); Hashmap.put ("D",4); List<map.entry<string, integer>> hashlist =NewArraylist<map.entry<string, integer>>(Hashmap.entryset ()); Collections.sort (Hashlist,NewComparator<map.entry<string, integer>>() {            //Sort Ascending             Public intCompare (entry<string, integer> O1, entry<string, integer>O2) {                returnO1.getvalue (). CompareTo (O2.getvalue ());        }        }); //post-order output         for(Map.entry<string, integer>m:hashlist) {System. out. println ("key="+ M.getkey () +", value="+M.getvalue ()); }        //list<integer> SortList<integer> nums =NewArraylist<integer>(); Nums.add (3); Nums.add (5); Nums.add (2); Nums.add (1); //Sort Ascending (default)Collections.sort (nums); //post-order outputSystem. out. println (Nums); //list<bean> Sortlist<user> users =NewArraylist<user>(); Users.add (NewUser (2,"Jack")); Users.add (NewUser (1,"Tom")); Users.add (NewUser (3,"Keck")); Users.add (NewUser (4,"Tao")); //ID Ascending SortCollections.sort (users); //post-order output         for(User user:users) {System. out. println (User.getid () +","+user.getname ()); }        //list<map> Sortlist<map<string, integer>> listmap =NewArraylist<map<string, integer>>(); Map<string, integer> map =NewHashmap<>(); Map.put (" Age", -); Map.put ("Sex",1);        Listmap.add (map); Map<string, integer> map2 =NewHashmap<>(); Map2.put (" Age", in); Map2.put ("Sex",2);        Listmap.add (MAP2); Map<string, integer> map3 =NewHashmap<>(); Map3.put (" Age", *); Map3.put ("Sex",1);        Listmap.add (MAP3); //Sort by Map valueCollections.sort (Listmap,NewComparator<map<string, integer>>() {@Override Public intCompare (map<string, integer> O1, map<string, integer>O2) {                returnO1.Get(" Age"). CompareTo (O2.Get(" Age"));//Age Ascending Sort            }        }); //post-order output         for(Map<string, integer>M:listmap) {System. out. println (m); }    }}

2.list<user> sort of User.java class:

Package com.tao.test; Public classUser Implements Comparable<user>{        Private intID; PrivateString name;  PublicUser (intID, String name)        {super ();  This. ID =ID;  This. Name =name; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intgetId () {returnID; }     Public voidSetId (intID) { This. ID =ID; } @Override Public intcompareTo (User o) {return  This. Id-o.getid ();//ID Ascending Sort    }}

3. Running

The Java language uses Collections.sort to sort map,list

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.