欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

map 排序

程序员文章站 2024-02-17 15:25:46
...

Map<Integer ,String> map = new HashMap<Integer,String>();
//map.put();省略 put
List arrayList = new ArrayList(map.entrySet());
Collections.sort(arrayList, new Comparator() {
public int compare(Object o1, Object o2) {
Map.Entry obj1 = (Map.Entry) o1;
Map.Entry obj2 = (Map.Entry) o2;
Collator cmp = Collator.getInstance(java.util.Locale.CHINA);//中文排序
return cmp.compare(obj1.getValue(), obj2.getValue());
}
});
Map<Integer,String> sortMap = new LinkedHashMap<Integer,String>();
for (Iterator iter = arrayList.iterator(); iter.hasNext();) {
Map.Entry entry = (Map.Entry) iter.next();
String value = (String)entry.getValue();
Integer key = (Integer) entry.getKey();
sortMap.put(key, value);
}


相关标签: java

上一篇: ES6 Array.from方法用法总结

下一篇: