既可滿足此類需求,原理很簡單,其重載的構造器之一有一個參數,該參數接受一個比較器,比較器定義比較規則,比較規則就是作用于TreeMap的鍵,據此可實現按鍵排序。publicMapsortMapBy" />

日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Java Map按鍵排序和按值排序

系統 2042 0

Map排序的方式有很多種,這里記錄下自己總結的兩種比較常用的方式:按鍵排序(sort by key), 按值排序(sort by value)。

?

按鍵排序(sort by key)

jdk內置的java.util包下的 TreeMap<K,V> 既可滿足此類需求,原理很簡單,其重載的構造器之一

有一個參數,該參數接受一個比較器,比較器定義比較規則,比較規則就是作用于 TreeMap<K,V> 的鍵,據此可實現按鍵排序。

?

?
        public Map<String, String> sortMapByKey(Map<String, String> oriMap) {  
    if (oriMap == null || oriMap.isEmpty()) {  
        return null;  
    }  
    Map<String, String> sortedMap = new TreeMap<String, String>(new Comparator<String>() {  
        public int compare(String key1, String key2) {  
            int intKey1 = 0, intKey2 = 0;  
            try {  
                intKey1 = getInt(key1);  
                intKey2 = getInt(key2);  
            } catch (Exception e) {  
                intKey1 = 0;   
                intKey2 = 0;  
            }  
            return intKey1 - intKey2;  
        }});  
    sortedMap.putAll(oriMap);  
    return sortedMap;  
}  
  
private int getInt(String str) {  
    int i = 0;  
    try {  
        Pattern p = Pattern.compile("^\\d+");  
        Matcher m = p.matcher(str);  
        if (m.find()) {  
            i = Integer.valueOf(m.group());  
        }  
    } catch (NumberFormatException e) {  
        e.printStackTrace();  
    }  
    return i;  
}  

      
?
?

按值排序(sort by value)

按值排序就相對麻煩些了,貌似沒有直接可用的數據結構能處理類似需求,需要我們自己轉換一下。

Map本身按值排序是很有意義的,很多場合下都會遇到類似需求,可以認為其值是定義的某種規則或者權重。

?

?
          public Map<String, String> sortMapByValue(Map<String, String> oriMap) {  
    Map<String, String> sortedMap = new LinkedHashMap<String, String>();  
    if (oriMap != null && !oriMap.isEmpty()) {  
        List<Map.Entry<String, String>> entryList = new ArrayList<Map.Entry<String, String>>(oriMap.entrySet());  
        Collections.sort(entryList,  
                new Comparator<Map.Entry<String, String>>() {  
                    public int compare(Entry<String, String> entry1,  
                            Entry<String, String> entry2) {  
                        int value1 = 0, value2 = 0;  
                        try {  
                            value1 = getInt(entry1.getValue());  
                            value2 = getInt(entry2.getValue());  
                        } catch (NumberFormatException e) {  
                            value1 = 0;  
                            value2 = 0;  
                        }  
                        return value2 - value1;  
                    }  
                });  
        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;  
}  

        
?

本例中先將待排序oriMap中的所有元素置于一個列表中,接著使用java.util.Collections的一個靜態方法

?

來排序列表,同樣是用比較器定義比較規則。排序后的列表中的元素再依次被裝入Map,需要注意的一點是為了肯定的保證Map中元素與排序后的List中的元素的順序一致,使用了LinkedHashMap數據類型,雖然該類型不常見,但是在一些特殊場合下還是非常有用的。

?

?

引用

Java Map按鍵排序和按值排序


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 汽车| 武宁县| 湘潭市| 神农架林区| 镇康县| 五家渠市| 阿拉尔市| 肥西县| 武川县| 即墨市| 柳林县| 玉山县| 白城市| 三穗县| 密云县| 同德县| 四平市| 通城县| 都安| 科技| 曲沃县| 永定县| 渭南市| 遵化市| 义乌市| 梁山县| 威信县| 天柱县| 阿坝县| 喜德县| 封开县| 慈利县| 仁化县| 临邑县| 罗源县| 遵化市| 荆州市| 阿巴嘎旗| 安国市| 万州区| 保靖县|