日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

Java Map按鍵排序和按值排序

 某某某1014 2014-03-29

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


按鍵排序(sort by key)

jdk內(nèi)置的java.util包下的TreeMap<K,V>既可滿足此類需求,原理很簡(jiǎn)單,其重載的構(gòu)造器之一


有一個(gè)參數(shù),該參數(shù)接受一個(gè)比較器,比較器定義比較規(guī)則,比較規(guī)則就是作用于TreeMap<K,V>的鍵,據(jù)此可實(shí)現(xiàn)按鍵排序。

  1. public Map<String, String> sortMapByKey(Map<String, String> oriMap) {  
  2.     if (oriMap == null || oriMap.isEmpty()) {  
  3.         return null;  
  4.     }  
  5.     Map<String, String> sortedMap = new TreeMap<String, String>(new Comparator<String>() {  
  6.         public int compare(String key1, String key2) {  
  7.             int intKey1 = 0, intKey2 = 0;  
  8.             try {  
  9.                 intKey1 = getInt(key1);  
  10.                 intKey2 = getInt(key2);  
  11.             } catch (Exception e) {  
  12.                 intKey1 = 0;   
  13.                 intKey2 = 0;  
  14.             }  
  15.             return intKey1 - intKey2;  
  16.         }});  
  17.     sortedMap.putAll(oriMap);  
  18.     return sortedMap;  
  19. }  
  20.   
  21. private int getInt(String str) {  
  22.     int i = 0;  
  23.     try {  
  24.         Pattern p = Pattern.compile("^\\d+");  
  25.         Matcher m = p.matcher(str);  
  26.         if (m.find()) {  
  27.             i = Integer.valueOf(m.group());  
  28.         }  
  29.     } catch (NumberFormatException e) {  
  30.         e.printStackTrace();  
  31.     }  
  32.     return i;  
  33. }  


按值排序(sort by value)

按值排序就相對(duì)麻煩些了,貌似沒(méi)有直接可用的數(shù)據(jù)結(jié)構(gòu)能處理類似需求,需要我們自己轉(zhuǎn)換一下。

Map本身按值排序是很有意義的,很多場(chǎng)合下都會(huì)遇到類似需求,可以認(rèn)為其值是定義的某種規(guī)則或者權(quán)重。

  1. public Map<String, String> sortMapByValue(Map<String, String> oriMap) {  
  2.     Map<String, String> sortedMap = new LinkedHashMap<String, String>();  
  3.     if (oriMap != null && !oriMap.isEmpty()) {  
  4.         List<Map.Entry<String, String>> entryList = new ArrayList<Map.Entry<String, String>>(oriMap.entrySet());  
  5.         Collections.sort(entryList,  
  6.                 new Comparator<Map.Entry<String, String>>() {  
  7.                     public int compare(Entry<String, String> entry1,  
  8.                             Entry<String, String> entry2) {  
  9.                         int value1 = 0, value2 = 0;  
  10.                         try {  
  11.                             value1 = getInt(entry1.getValue());  
  12.                             value2 = getInt(entry2.getValue());  
  13.                         } catch (NumberFormatException e) {  
  14.                             value1 = 0;  
  15.                             value2 = 0;  
  16.                         }  
  17.                         return value2 - value1;  
  18.                     }  
  19.                 });  
  20.         Iterator<Map.Entry<String, String>> iter = entryList.iterator();  
  21.         Map.Entry<String, String> tmpEntry = null;  
  22.         while (iter.hasNext()) {  
  23.             tmpEntry = iter.next();  
  24.             sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue());  
  25.         }  
  26.     }  
  27.     return sortedMap;  
  28. }  

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


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


    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多