java-map按key升序排序
阿新 • • 發佈:2019-01-07
/** * 使用 Map按key進行排序 * @param map * @return */ public static Map<Object, Object> sortMapByKey(Map<Object, Object> map) { if (map == null || map.isEmpty()) { return null; } Map<Object, Object> sortMap = new TreeMap<Object, Object>(new Comparator<String>(){ public int compare(String str1,String str2){ return str1.compareTo(str2)//將str1和str2調換位置是倒序排序 } }); sortMap.putAll(map); return sortMap; }
例如~:
Map priceMap=new TreeMap();
priceMap.put("","");
priceMap.put("","");
priceMap.put("","");
priceMap.put("","");
priceMap.put("","");
Map<String, String> valueMap = sortMapByKey(priceMap); //按Key進行排序
ok 搞定~