免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
Java Map按鍵排序和按值排序

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


按鍵排序(sort by key)

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


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

  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)

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

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

  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中的所有元素置于一個列表中,接著使用java.util.Collections的一個靜態(tài)方法


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


本站僅提供存儲服務,所有內容均由用戶發(fā)布,如發(fā)現有害或侵權內容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
SortedMap接口的實現類TreeMap介紹和實現Comparator自定義比較器 | IT宅.com
Map
HashMap遍歷
(C++)STL中map按照vaule來排序
關于Map
Map類型取值方法
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服