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类型,可设置按照从大到小还是从小到大排序