Map的遍历
for (Map.Entry<String, String> m : stringObjectMap.entrySet()) {
System.out.println("key:" + m.getKey() + " value:" + m.getValue());
}
Map根据key对Map排序
public static Map<String, String> sortMapByKey(Map<String, String> map) {
if (map == null || map.isEmpty()) {
return null;
}
//定义TreeMap 重写其 Comparator
Map<String, String> sortMap = new TreeMap<String, String>(
new Comparator<String>() {
//升序排序,降序排序为 o2.compareTo(o1);
public int compare(String o1, String o2) {
return o2.compareTo(o1);
}
}
);
//将 Map 传入 TreeMap 中,执行比较方法
sortMap.putAll(map);
return sortMap;
}