One: Causes:
(1) In reality, it is necessary to sort the map container in many cases: because the storage structure of the Map<key,value> key-value pair is very excellent, especially the HASHMAP structure, the data storage will inevitably be sorted;
(2) Data processing, as long as the mapping relationship can not be separated from the map, which is very practical in data processing, and sequencing is the further processing of data;
(3) There are a number of ways to sort the map, two more common ways: Sort by key, sorted by value (sort by value)
Two: Algorithms for sorting
(1) Key sorting
The treemap<k,v> in the JDK's built-in java.util package satisfies such requirements and constructs methods TreeMap (COMPARATOR<? Super K> Comparator) The key ordering is achieved by passing in our custom comparators.
//main class public classes Mapsortdemo {public static void main (string[] args) {map<string, string> map = new treemap<string, string> () map.put ("KFC", "KFC"), Map.put ("WNBA", "WNBA"); Map.put ("NBA", "NBA "); Map.put (" CBA "," CBA "); map<string, string> resultmap = Sortmapbykey (map);//Sort by key for (Map.entry<string, string> Entry: Resultmap.entryset ()) {System.out.println (Entry.getkey () + "" + Entry.getvalue ())}} /** * Use map to sort by key * @param map * @return */public static map<string, string> Sortmapbykey (map<string, STRING&G T MAP) {if (map = = NULL | | map.isempty ()) {return null;} map<string, string> sortmap = new treemap<string, string> (New Mapkeycomparator ()); Sortmap.putall (map); return sortmap;}} Comparator class public class Mapkeycomparator implements Comparator<string>{public int Compare (string str1, String str2) { Return Str1.compareto (STR2);}}
Description One: The comparator class, Mapkeycomparator implements comparator<string> parameters are through similar template class
comparator<> Pass in, and then reload its compare () function, it is relatively simple.
Description Two: The Sortmapbykey (map<string, string> Map) function in the main class declares treemap<string,string> (new Mapkeycomparator ()) Sort the key value , and then call Sortmap.putall (map) to copy to the new map.
Description Three: The data is stored with TreeMap.
(2) Sort by value
Sort by value is relatively troublesome, seemingly no data structure directly available to handle similar requirements, we need to switch.
The map itself is meaningful to sort by value, and in many cases a similar requirement can be thought of as a defined rule or weight.
Principle: Place all the elements in the map to be sorted into a list, then use a static method of collections sort (list<t> list, comparator<? Super T> C)
to arrange the list, the comparison rules are also defined by the comparator. the elements in the sorted list are then loaded into the map in order to ensure that the elements in the map are in the same order as the elements in the ordered list, using the
Linkedhashmap Data types
Implementation code
Main class public class Mapsortdemo {public static void main (string[] args) {map<string, string> Map = new Treemap<stri Ng, String> (); Map.put ("KFC", "KFC"), Map.put ("WNBA", "WNBA"), Map.put ("NBA", "NBA"), Map.put ("CBA", "CBA"); map<string, string> resultmap = sortmapbyvalue (map); Sort by value for (map.entry<string, string> entry:resultMap.entrySet ()) {System.out.println (Entry.getkey () + "" + Entry.getvalue ());}} /** * Use map to sort by value * @param map * @return */public static map<string, string> Sortmapbyvalue (map<string, Stri Ng> map) {if (map = = NULL | | map.isempty ()) {return null;} map<string, string> sortedmap = new linkedhashmap<string, string> (); list<map.entry<string, string>> entrylist = new arraylist<map.entry<string, String>> ( Map.entryset ()); Collections.sort (Entrylist, New Mapvaluecomparator ()); iterator<map.entry<string, string>> iter = Entrylist.iterator (); map.entry<string, string> tmpentry = Null;whilE (Iter.hasnext ()) {tmpentry = Iter.next (); Sortedmap.put (Tmpentry.getkey (), Tmpentry.getvalue ());} return sortedmap;}} Comparator class public class Mapvaluecomparator implements Comparator<map.entry<string, string>> {public int compare (Entry<string, string> me1, entry<string, string> me2) {return Me1.getvalue (). CompareTo (Me2.getvalue ());}}
Description One: The comparator class, the parameters in the Mapvaluecomparator implements comparator<map.entry<string,string>> are through a similar template class Comparator <> Pass in, and then reload its compare () function, only the parameter is the interface class of map.entry<string,string>, compare () function of the two parameters are correspondingly changed to entry<string, String>.
Description Two: Sortmapbykey in the main class (Map<string, string> Map) function declared linkedhashmap<string,string> re-use arraylist< Map.entry<string,string>> (Map.entryset ()) is stored in list form and then called Collections.sort (Entrylist,
New Mapkeycomparator ()), and finally turn entrylist into Linkedmap.
Description Three: The data is stored with TreeMap.
Sort Java Map (key,value)