Map排序,
public List<Map.Entry<Integer, String>> mapSort(Map map){
List<Map.Entry<Integer, String>> infoIds = new ArrayList<Map.Entry<Integer, String>>(
map.entrySet());
Collections.sort(infoIds, new Comparator<Map.Entry<Integer,String >>() {
public int compare(Map.Entry<Integer, String> o1,
Map.Entry<Integer, String> o2) {
return Collator.getInstance(Locale.CHINESE).compare(o1.getValue(), o2.getValue());
}
});
return infoIds;
}
public List<Map.Entry<String, String>> mapStringSort(Map map){
List<Map.Entry<String, String>> infoIds = new ArrayList<Map.Entry<String, String>>(
map.entrySet());
Collections.sort(infoIds, new Comparator<Map.Entry<String,String >>() {
public int compare(Map.Entry<String, String> o1,
Map.Entry<String, String> o2) {
return Collator.getInstance(Locale.CHINESE).compare(o1.getValue(), o2.getValue());
}
});
return infoIds;
}
set排序,非常简单,实现一个treeSet即可
Set<Integer> treeSet = new TreeSet();
treeSet.addAll(depIdSet);
//depIdSet 是一个无序的set,
//最好的办法是在生产depIdSet时就用treesSet存储
本文介绍如何在Java中对Map和Set进行排序。针对Map,提供了两种不同类型的键值对排序方法;对于Set,则介绍了使用TreeSet来实现排序的简单方式。这些技巧对于需要对集合数据进行有序处理的应用场景特别有用。
352

被折叠的 条评论
为什么被折叠?



