There are a number of ways to sort map, two more common: sort by key, sorted by value (sort by value).
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.
Java code
- Public class 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> 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);
- }
- }
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 map, in order to ensure that the elements in the map match the order of the elements in the sorted list, using the Linkedhashmap data type.
Java code
- Implementation code
- Public class 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 = 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, string> 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 ());
- }
- }
Java map key (key) sorting and sorting by values (value)