Java map key (key) sorting and sorting by values (value)

Source: Internet
Author: User

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
  1. Public class Mapsortdemo {
  2. public static void Main (string[] args) {
  3. map<string, string> map = new treemap<string, string> ();
  4. Map.put ("KFC", "KFC");
  5. Map.put ("WNBA", "WNBA");
  6. Map.put ("NBA", "NBA");
  7. Map.put ("CBA", "CBA");
  8. map<string, string> resultmap = Sortmapbykey (map); //Sort by key
  9. for (map.entry<string, string> entry:resultMap.entrySet ()) {
  10. System.out.println (Entry.getkey () + "" + Entry.getvalue ());
  11. }
  12. }
  13. /** 
  14. * Use map to sort by key
  15. * @param map
  16. * @return
  17. */
  18. public static map<string, string> Sortmapbykey (map<string, string> Map) {
  19. if (map = = Null | | map.isempty ()) {
  20. return null;
  21. }
  22. map<string, string> sortmap = new treemap<string, string> (new Mapkeycomparator ());
  23. Sortmap.putall (map);
  24. return sortmap;
  25. }
  26. }
  27. Comparator class
  28. Public class Mapkeycomparator implements comparator<string>{
  29. public int Compare (string str1, String str2) {
  30. return Str1.compareto (STR2);
  31. }
  32. }

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
    1. Implementation code
    2. Public class Mapsortdemo {
    3. public static void Main (string[] args) {
    4. map<string, string> map = new treemap<string, string> ();
    5. Map.put ("KFC", "KFC");
    6. Map.put ("WNBA", "WNBA");
    7. Map.put ("NBA", "NBA");
    8. Map.put ("CBA", "CBA");
    9. map<string, string> resultmap = sortmapbyvalue (map); //Sort by value
    10. for (map.entry<string, string> entry:resultMap.entrySet ()) {
    11. System.out.println (Entry.getkey () + "" + Entry.getvalue ());
    12. }
    13. }
    14. /** 
    15. * Use map to sort by value
    16. * @param map
    17. * @return
    18. */
    19. public static map<string, string> Sortmapbyvalue (map<string, string> Map) {
    20. if (map = = Null | | map.isempty ()) {
    21. return null;
    22. }
    23. map<string, string> sortedmap = new linkedhashmap<string, string> ();
    24. list<map.entry<string, string>> entrylist = new arraylist<map.entry<string, String>> (  Map.entryset ());
    25. Collections.sort (Entrylist, new Mapvaluecomparator ());
    26. Iterator<map.entry<string, string>> iter = Entrylist.iterator ();
    27. map.entry<string, string> tmpentry = null;
    28. While (Iter.hasnext ()) {
    29. Tmpentry = Iter.next ();
    30. Sortedmap.put (Tmpentry.getkey (), Tmpentry.getvalue ());
    31. }
    32. return sortedmap;
    33. }
    34. }
    35. Comparator class
    36. Public class Mapvaluecomparator implements Comparator<map.entry<string, string>> {
    37. public int Compare (entry<string, string> me1, entry<string, string> me2) {
    38. return Me1.getvalue (). CompareTo (Me2.getvalue ());
    39. }
    40. }

Java map key (key) sorting and sorting by values (value)

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.