在java中怎樣對一個Map進行排序(java 8之前的版本)

來源:互聯網
上載者:User

 幾個對一個Map的keys或者values進行排序的java例子. 注意:
如果你使用的是 Java 8, 參考這篇文章 –  Java 8 – 怎樣對Map排序  1. 按照key排序

1.1 用 java.util.TreeMap, 它將自動根據keys對Map進行排序. SortByKeyExample1.java

package com.mkyong.test;import java.util.HashMap;import java.util.Map;import java.util.TreeMap;public class SortByKeyExample1 {    public static void main(String[] args) {        Map<String, String> unsortMap = new HashMap<String, String>();        unsortMap.put("Z", "z");        unsortMap.put("B", "b");        unsortMap.put("A", "a");        unsortMap.put("C", "c");        unsortMap.put("D", "d");        unsortMap.put("E", "e");        unsortMap.put("Y", "y");        unsortMap.put("N", "n");        unsortMap.put("J", "j");        unsortMap.put("M", "m");        unsortMap.put("F", "f");        System.out.println("Unsort Map......");        printMap(unsortMap);        System.out.println("\nSorted Map......By Key");        Map<String, String> treeMap = new TreeMap<String, String>(unsortMap);        printMap(treeMap);    }    //pretty print a map    public static <K, V> void printMap(Map<K, V> map) {        for (Map.Entry<K, V> entry : map.entrySet()) {            System.out.println("Key : " + entry.getKey()+ " Value : " + entry.getValue());        }    }}

Output

Unsort Map......Key : A Value : aKey : B Value : bKey : C Value : cKey : D Value : dKey : E Value : eKey : F Value : fKey : Y Value : yKey : Z Value : zKey : J Value : jKey : M Value : mKey : N Value : nSorted Map......By KeyKey : A Value : aKey : B Value : bKey : C Value : cKey : D Value : dKey : E Value : eKey : F Value : fKey : J Value : jKey : M Value : mKey : N Value : nKey : Y Value : yKey : Z Value : z

1.2 另一個 java.util.TreeMap 例子 ,提供一個自訂的 Comparator 對可以進行降序排序. SortByKeyExample2.java

package com.mkyong.test;import java.util.Comparator;import java.util.HashMap;import java.util.Map;import java.util.TreeMap;public class SortByKeyExample2 {    public static void main(String[] args) {        Map<Integer, String> unsortMap = new HashMap<Integer, String>();        unsortMap.put(10, "z");        unsortMap.put(5, "b");        unsortMap.put(6, "a");        unsortMap.put(20, "c");        unsortMap.put(1, "d");        unsortMap.put(7, "e");        unsortMap.put(8, "y");        unsortMap.put(99, "n");        unsortMap.put(50, "j");        unsortMap.put(2, "m");        unsortMap.put(9, "f");        System.out.println("Unsort Map......");        printMap(unsortMap);        System.out.println("\nSorted Map......By Key");        Map<Integer, String> treeMap = new TreeMap<Integer, String>(                new Comparator<Integer>(

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.