A. 如何對Map進行字典排序java
//根據 Map的key進行字典排序
Map<String, String> parameterMap = new HashMap<String, String>();
List<Map.Entry<String,String>> list = new ArrayList<Map.Entry<String,String>>(parameterMap.entrySet());
Collections.sort(list,new Comparator<Map.Entry<String,String>>() {
//升序排序
public int compare(Entry<String, String> o1,
Entry<String, String> o2) {
return o1.getKey().compareTo(o2.getKey());
}
});
B. java map 怎麼按按key排序
java中map按照double大小進行排序,可以使用collentions介面的sort方法進行比較,實例如下:
Collections.sort(dataMap, new Comparator<Map<String, Object>>() { @Override public int compare(Map<String, Object> lhs, Map<String, Object> rhs) { Double d5 = ((Double) rhs.get(OpenPrice)); Double d6 = (Double) lhs.get(OpenPrice); if (d5 != null && d6 != null) { return d5.compareTo(d6); } else { return flag; } // return d1.compareTo(d2);}
根據返回的int類型,可設置按照從大到小還是從小到大排序