怎么实现对Map的值进行排序?
来源:互联网 发布:java中excel导入导出 编辑:程序博客网 时间:2024/05/16 05:29
我们知道Map是以键值对的接口,他的实现子类主要是:
1、Hashtable:底层是哈希表数据结构,不可以存入空键和空值,线程是同步的,在JDK1.0版本出现,
2、HashMap:底层是哈希表数据结构,可以存入空键和空值,线程是不同步的,在JDK1.2版本出现所以效率方面比Hashtable高3、TreeMap:底层是二叉树数据结构,支持键的自然排序,线程是不同步的,
按key排序:
class Demo{
public static void main(String[] args){
TreeMap<String,String> map = new TreeMap<String,String>(new Comparator<String>(){
public int compare(String a,String b){
return a.compareTo(b);
}
});
map.put("b","B");
map.put("a","A");
map.put("c","C");
map.put("d","D");
for(Map.Entry<String, String> me :map.entrySet()){
System.out.println(me.getKey()+":"+me.getValue());
}
}
}
按value排序:
class Demo{
public static void main(String[] args){
HashMap<Integer,String> map = new HashMap<Integer,String>();
map.put(new Integer(2), "a");
map.put(new Integer(5), "c");
map.put(new Integer(1), "b");
map.put(new Integer(3), "aa");
List<Map.Entry<Integer,String>> li = new ArrayList<Map.Entry<Integer,String>>(
map.entrySet());
Collections.sort(li,new Comparator<Map.Entry<Integer,String>>(){
public int compare(Entry<Integer,String> a,Entry<Integer,String> b){
return a.getValue().compareTo(b.getValue());
}
});
for(Map.Entry<Integer,String> me : li){
System.out.println(me.getKey()+":"+me.getValue());
}
}
}
- 怎么实现对Map的值进行排序?
- Java 对Map进行排序的实现方法
- C++中实现对map按照value值进行排序
- 对Map进行排序
- 对map里的数据进行排序
- 根据value值对map进行排序
- 对元素为map的list按map中的值进行排序
- java 对map进行排序
- 对map键值进行排序
- Map对value进行排序
- 对List<Map>进行排序
- c++对map进行排序
- 对map集合进行排序
- 对map集合进行排序
- 对map集合进行排序
- 对map集合进行排序
- 对map集合进行排序
- c++对map进行排序
- sd 协议解析
- 原生地图获取坐标转化为真实坐标,火星坐标
- 高仿知乎日报(四)[主页面条目显示]
- cout函数控制结果输出形式
- 汇编(二)8086指令系统
- 怎么实现对Map的值进行排序?
- 文章标题
- ADB常用命令与所遇问题解决
- JDBC连接SQL Server2008 R2
- 京东幸运数1
- 百分比布局
- 用wifi链接手机,解放你的数据线!
- 住得越高越安静? 中间楼层噪音最大
- freemarker 自定义标签