Java map sorted by value

Source: Internet
Author: User
Tags sorts stub

Map is a set interface of key-value pairs, and its implementation classes mainly include: Hashmap,treemap,hashtable and Linkedhashmap.

TREEMAP: Based on the navigablemap implementation of the red-black tree (red-black trees), the map is sorted according to the natural order of its keys or sorted based on the Comparator provided when the mapping was created, depending on the construction method used.
The value of HashMap is not sequential, it is according to the key hashcode to achieve, for this unordered hashmap how do we want to implement the sort? Sorts the value of reference TreeMap.


Map.entry returns the collections view.

Sort by key

TreeMap is ascending by default, and if we need to change the sorting method, we need to use the comparator: Comparator. Comparator a comparer interface that can sort a collection object or array, implementing the public compare (T O1,to2) method of the interface to achieve sorting.

Note: The following code has been passed in the Jdk1.6 test

TreeMap by key in ascending order by default

Public static void keyupsort ()  {        //  By default, TreeMap is sorted by key in ascending order         map<string, integer> map  = new TreeMap<String, Integer> ();         Map.put ("ACB1",  5);         map.put ("Bac1",  3);         map.put ("BCA1",  20);         Map.put ("Cab1",  80);         map.put ("Cba1",  1);         map.put ("ABC1",  10);         Map.put ("ABC2",  12);        //  by default, TreeMap sort keys in ascending order          system.out.println ("------------normal situation, TreeMap sorted by key in ascending order--------------------");         for  (map.entry<string, integer> entry :  Map.entryset ())  {             System.out.println (Entry.getkey ()  +  ":"  + entry.getvalue ());         }    }


Modify the Sort method for TreeMap, sort by key descending

Public static void keydownsort ()  {        //  TreeMap, sort by key         //  descending sort comparer          Comparator<String> keyComparator = new Comparator<String> ()  {             @Override              public int compare (String o1, String &NBSP;O2)  {                 // TODO Auto-generated method stub                 return o2.compareto (O1);             }        };         Map<String, Integer> map = new TreeMap<String, Integer> ( Keycomparator);         map.put ("ACB1",  5);         map.put ("Bac1",  3);         map.put ( "BCA1",  20);         map.put ("Cab1",  80);         map.put ("Cba1",  1);         map.put ( "ABC1",  10);         map.put ("ABC2",  12);         system.out.println ("------------treemap sorted by key in descending order--------------------");         for  (map.entry<string, integer> entry :  map.entryset ())  {             System.out.println (Entry.getkey ()  +&nBSP; ":"  + entry.getvalue ());        }     }

Sort by value



modifies the sort method of TreeMap, sorted by value ascending

Note: Normally, map cannot be sorted using the Collections.sort () method, but you can convert the map to list before sorting.

Public static void valueupsort ()  {        //  By default, TreeMap is sorted by key in ascending order         map<string, integer> map  = new TreeMap<String, Integer> ();         Map.put ("ACB1",  5);         map.put ("Bac1",  3);         map.put ("BCA1",  20);         Map.put ("Cab1",  80);         map.put ("Cba1",  1);         map.put ("ABC1",  10);         Map.put ("ABC2",  12);        //  Ascending Comparator          comparator<map.entry<string, integer>> valuecomparator =  new comparaTor<map.entry<string,integer>> ()  {              @Override             public  int compare (entry<string, integer> o1,                     entry<string, integer &GT;&NBSP;O2)  {                 // TODO Auto-generated method stub                 return o1.getvalue ()-o2.getvalue ();             }        };         // map Convert to list sort          list<map.entry<string,&Nbsp;integer>> list = new arraylist<map.entry<string,integer>> ( Map.entryset ());        //  sort          collections.sort (List,valuecomparator);        //  By default, TreeMap sorts keys in ascending order         system.out.println ("------------ Map is sorted in ascending order of Value--------------------");        for  (map.entry< String, integer> entry : list)  {             system.out.println (Entry.getkey ()  +  ":"  + entry.getvalue ());         }    }

Test results

------------Normal, TreeMap is sorted by key in ascending order--------------------abc1:10abc2:12acb1:5bac1:3bca1:20cab1:80cba1:1------------ TreeMap sort by key in descending order--------------------cba1:1cab1:80bca1:20bac1:3acb1:5abc2:12abc1:10------------ The map is sorted in ascending order of value--------------------cba1:1bac1:3acb1:5abc1:10abc2:12bca1:20cab1:80


This article is from the "Craneyuan" blog, make sure to keep this source http://craneyuan.blog.51cto.com/9365548/1838256

Java map sorted by value

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.